Main Contents Page
CTEC1401 Programming in C
Quick Reference
Week 14: Consolidation
Week 15: Strings
Week 16: Functions I
  1. Before the lab session read CFTTP Chapters 9 and 10.

  2. string1

    Write a C program called string1.c that reads in a string of no more than 80 characters and then prints it back out (within double quotes).

    Your string? C programming is fun
    You entered: "C programming is fun"
    
  3. string2

    Write a C program called string2.c that reads in a string of no more than 80 characters and then prints it back out (within double quotes) with all spaces replaced by stars.

    Your string? C programming is fun
    Modified to: "C*programming*is*fun"
    
  4. string3

    Write a C program called string3.c that reads in a string of no more than 80 characters and then prints it back out (within double quotes) with all lower case characters changed to upper case.

    Your string? C programming is fun
    Modified to: "C PROGRAMMING IS FUN"
    
  5. string4

    Write a C program called string4.c that reads in a string of no more than 80 characters and a number. The string should be modified by applying the Caeser cipher to every lower case character in the string. The number is used to control the encryption (i.e. the amount of character rotation). Once encrypted the string should be printed out.

    Your string? Hello! I like C
    Rotation   ? 1
    Modified to: "Hfmmp! I mjlf C"
    
  6. string5

    Write a C program called string5.c that reads in a string of no more than 80 characters and prints it out backwards.

    Your string? Hello! I like C
    Modified to: "C ekil I !olleH"
    
  7. morse

    Write a C program morse.c that gets an input string of text characters from the user and generates a new string containing the message in Morse code.

    A "dot" is represented by the character 'o' and a dash by three hyphen characters '---'. The space between the parts of a single letter is represented by a single space ' '; the space between letters is represented by three space characters '   '; the space between words is represented by seven space characters '       '. For example, the string "I like C" would become:

    o o       o --- o o   o o   --- o ---   o       --- o --- o
    

    The program should also output the overall length of the generated Morse code message (i.e. the length of the encoded string). In the example the length of the encoded string is 59.

  8. charcount

    Write a C program charcount.c that

    1. reads in a string of no more than 255 characters from the user;
    2. counts how many instances there are of each letter in the string; (note, for this program we ignore case so, e.g., the number of letter 'a' characters includes the total number of lower case 'a's and upper case 'A's.)
    3. prints out the resulting count for each letter.
    i.e. if the user enters "Time and Tide Wait for No Man", the program should print
     a: 3  b: 0  c: 0  d: 2  e: 2  f: 1  g:  0  h: 0  i:  3  j:  0  k:  0  l:  0  m:  2
     n: 3  o: 2  p: 0  q: 0  r: 1  s: 0  t:  3  u: 0  v:  0  w:  1  x:  0  y:  0  z:  0
    

    Hint: This program is a bit trickier than the others so here is an outline of the processing required

    1. declare a char array to store the string
    2. declare an int array of size 26 to store the count for each letter e.g. count[0] is the number of a's, count[1] the number of b's etc.
    3. read in the string and then process it element by element
    4. if the element is not an alphabetic character i.e. A -Z or a - z, ignore it
    5. otherwise, convert it to a number between 0 and 25 (i.e. a or A become 0, z or Z become 25) and update the corresponding count.

  9. getwords

    Write a C program getwords.c that gets an input string of text characters from the user and prints out all the separate words in the text.

    A word is defined as a sequence of characters terminated by punctuation or whitespace. For example, the input

    Into the valley of death rode the six hundred.
    
    would produce the output:

    Into
    the
    valley
    of
    death
    rode
    the
    six
    hundred
    

    This kind of processing is a requirement of any compiler or spell checker.