Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming Exercises
Back to top
On this page
Contents

Simple text exercises

Remember that a "writeable string" is just an array of characters and that at this stage we assume that anything read from the keyboard such names etc. do not have spaces. You may assume that all names are 10 characters or less.

Read in a name, print it out

  1. Declare a character array, ask for a person's name and read it in.
  2. Print it out to the screen.

Several names

  1. Replace the single character array by an array of N character arrays.
  2. Read in all N names, then print them all out.
  3. Ask for an integer in the range 1to N and print out that name, ie. if they enter 1 print out the first name.
  4. Ask for a second integer in the range 1+ and if it is less than or equal to the length of the name just printed then print out that character of that name.

A function

Split the above into two parts: read in the names inside main() then pass (the address of) the two-dimensional array to a function to do the second part.

A length comparision

Now write a second function that also gets passed (the address of) the two-dimensional array. Have this function ask for a minimum length and print out all the names this long or longer.

An alphbetical comparision

Now write a third function that also gets passed (the address of) the two-dimensional array. Have this function print out every name that is alphabetically before the first one. You should use strcmp().

                                                                                                                                                                                                                                                                       

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