Skip to content
School of Physics
Home Our Teaching Resources C programming ex7.html
Back to top

Exercises 7

These exercises are assessed. In all cases hand in your code and the printout of the final result.

The aim is to gain experience of messing around with text. As always, you may use any example functions in the lecture notes but you should make sure you understand what they are doing. Notice that when we say "write a function to .." that is what we mean!!

  1. Inside main() declare two character arrays (which can be used used to store modifiable strings) of the same length.

  2. Write a simple menu function to return one of a number of choices. Add each of the folowing choices one at a time, make sure each is working correctly before starting on the next. For each choice have a separate function called from main() to perform the action. Have the whole "menu and perform the action" code inside an infinite loop.

    Each option, except quit, will involve reading in a string. For simplicity use strings without spaces but use the underscore character '_' instead of a space.

  3. Menu options:
    1. Quit the program.
    2. Print out the number of upper case letters in the string.
    3. Safely copy the first string to the second, and in the copied string replace each character that is neither a letter or a number with a star "*".
    4. See if the string contains the substring "wobble".
    5. See if the string is alphbetically before, after or the same as string "bobble".
    6. Safely set the second string to be:
      "You entered: " followed by the original string, for example: "You entered: hello, world".

    In each case by "safely" we mean to only copy as much of the string as can be safely held in the destination. Make sure you provide some output showing that it does work safely.

  4. Now modify your code so that when you read in a character string it then gets modified to replace every underscore with a space.

    How convenient is this for the user? [Not very!]. We will see another approach next week.

                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top