In Search of Palindromes.

February 1998

A palindrome is a word, a phrase, or a sentence that can be read both the usual way (from left to right) and backwards without changing its meaning. There are many palindromic words, such as radar, madam and eye. There are phrases that are perfectly palindromic ( Dennis sinned ), but usually we agree to ignore spaces (word boundaries) and punctuation, and look only at the letters themselves, so that Never odd or even, or Rise to vote, Sir! are palindromes too.

You will write a program that decides if a string is a palindromic sentence, and if it is a perfect palindrome (i.e. word boundaries are preserved by reading it backwards). At first you will enter your test phrases from standard input, then take them from a file.

To check whether a sentence is palindromic, you need to change all letters to lowercase and then compactify the string by removing all spaces and punctuation (leaving only a string of lowercase letters). Then you need to check if the first character in the condensed string is the same as its last character, the same for the second and last-but-one characters, and so on.

C++ strings are described in detail on pages 91-93 of your textbook. Files are described in Chapter 11 (see 11.4), and you can use my Files Example program in the Examples folder in the course directories. Start your work by dragging the Minimal Project folder to your desktop. If you want to use any of the nice CoreTools library functions, duplicate one of the existing projects (Files Example will do) and work in it instead. Don't forget to put #include <string> and #include <fstream.h> at the head of your program if you are starting with the Minimal project.

Proceed as follows:

  1. Write a function bool IsLetter ( char ch ) which determines if a character is a letter and returns true or false accordingly.
  2. Write a function string ToLowerCase ( string s ) which takes a string s and returns the string which is just like s , but all letters in it are lowercase (all spaces and punctuation are still preserved). You can use this string for testing if you have a perfect palindrome, like Reflog a golfer.
  3. Write a function string CondenseString( string s ) which takes a string s and returns the string that consists only of letters from s (spaces and punctuation are removed).
  4. Test these functions to make sure they work correctly.
  5. Write a function bool IsPalindrome (string s) which takes a sentence as a string and checks if it is a palindrome. Let it call the functions above for pre-processing the string before the actual test. Test it now, by giving it some known palindromes.

Now you are ready to take input. Use

getline( cin, your_string );

to input sentences from the keyboard ( cin >> your_string; stops at the first whitespace, whereas you need to input strings containing spaces).

  1. In your main(), read phrases in a loop, for each phrase print out whether the phrase is a palindrome or a perfect palindrome
  2. Now read phrases from a file rather than from the standard input. For this you will need to create an ifstream MyFile, connect it to a file with MyFile.open("file_name") and read lines from it with

getline( MyFile, your_string );.

Put the test file into your project folder -- that way you don't have to show the full path to it, just the file name is OK. Refer to my Files Example in the course directories.

I recommend that you look at http://stekt.oulu.fi/~jopi/humor/palindromes.html

for more information and fun facts about palindromes (longest known palindromes, palindromic verse, palindromes in other languages etc.). Also, this is a good page to find more palindromes for your tests.