# ESc 014

## Assignment 5 -- Palindrome Checker Due Tuesday, May 14, 2002

### Objectives

• To practice recursion

Your task is to write a program that uses a recursive function to compute whether an input sentence is a palindrome. A palindrome is a word or phrase that reads the same forward as backwards, ignoring spaces, punctuation, and capitalization. Examples include: noon, Racecar!, A man, a plan, a canal...Panama!, and Madam, I'm Adam.

Write a program that asks the user to input a word or phrase and stores it as a string. The normal method of inputting a string won't accept spaces, so instead, you should use the getline function as detailed on p. 147 of your text.

Your program will then use a recursive function called palindromeChecker to determine if the user's input is a palindrome or not. This function will return true if the user's input is indeed a palindrome and false otherwise. You will then display a message to the user telling whether the user's input was a palindrome. Because you will use recursion to solve this problem, the palindromeChecker function should not have any loops in it of any kind.

Remember, a recursive solution is one that defines the problem in terms of itself. So to help you write the palindromeChecker function, here is a recursive description of what the function should do and how it figures out whether a string is a palindrome or not:

1. If a sentence is zero or one letters long, it is a palindrome.
2. Otherwise, if a sentence starts with a non-letter (something that is not A-Z or a-z), then it is a palindrome if the sentence minus the first character is a palindrome.
3. Otherwise, if a sentence ends with a non-letter, then it is a palindrome if the sentence minus the last character is a palindrome.
4. Otherwise, if the upper-case versions of the first and last letters are the same, the sentence is a palindrome if the sentence minus the first and last characters is a palindrome.
5. Otherwise, the sentence is not a palindrome.
As always, you should use other functions besides palindromeChecker to help break this problem down into smaller chunks. Here are the other functions you are required to use. All of them should be called from palindromeChecker at the appropriate time:
• a function called firstCharIsLetter that takes the user's input sentence as a parameter. This function returns true if the sentence's first character is a letter and false otherwise.
• a function called lastCharIsLetter that takes the user's input sentence as a parameter. This function returns true if the sentence's last character is a letter and false otherwise.
• a function called firstAndLastAreEqual that takes the user's input sentence as a parameter. This function returns true if the upper case version of the first character is equal to the upper case version of the last character. It returns false otherwise.
Come talk to me early on if you're not sure how these auxiliary functions fit in with the palindromeChecker function! And use these "English" explanations of the different functions to help you flesh out what the C++ code should be. You'll need to make use of string functions in the <string> library and character functions in the <cctype> library to write your program. In your text, see p. 150 for a review of the string functions and p. 356 for a review of the character functions.

50 points will be divided as follows:
• 25 points for a working program
• 10 points for proper use of recursion
• 10 points for appropriate documentation, spacing, testing, and C++ language usage. Be sure to do adequate testing! Use phrases that have a variety of punctuation, spaces, and both upper and lower case characters to make sure your code works under all conditions! I'm sure a web search will yield many palindrome examples if you can't think of any more yourself.
• 5 points for appropriate function usage

As always, paste in your test results at the bottom of your source code and turn in a hard copy of this assignment along with an electronic copy on BlackBoard.