Processing Strings as Character Arrays – Part I

The next big step is to write a function called testForPalindrome, which does what its name obviously suggests. However, we will write another helper function first, to make testForPalindrome much easier to design. So in this step, we will first write a function called convertToCanonical(), which takes a single C-string (i.e., an array of chars), and will preprocess the string to turn it into a canonical form, by doing the following 3 conversions:

  1. Strip out spaces (that is the only whitespace we will worry about)
  2. Strip out punctuation (we will limit ourselves to commas, periods, exclamation marks, and question marks)
  3. Convert all uppercase letters to lowercase

The function modifies its argument, so it doesn't need to return a value, and should be declared "void".

So, if convertToCanonical() is given the string: "Hello , world ! ", it should return: "helloworld"
.

Something important to note about the char array passed in: we do not have to also pass in the size of the array as we've done elsewhere. This is because:

  1. It contains a proper C-string, meaning it is null-terminated; and
  2. the modified string is guaranteed to be equal to or shorter than the original string
This means that if you are clever, you can do the conversion completely within the array that was passed in, without needing another array. However, if you would like to use another array, you can declare one locally using "char tmpArray[MAX_STR]". Make sure you copy the final results back into the array that was passed in, though.