C++ Assignment 14
Palindromes

This assignment asks you to write a program that checks to see if a line of input is a Palindrome. A Palindrome is a word or phrase that is the same when read forward or backward.

Program Requirements

Your finished program should
  1. correctly determine if a string input by the user is a palindrome.
  2. include sample output. Use the input shown above as a guide.
  3. ignore all spaces, punctuation marks, apostrophes, and other non-alphanumeric characters.
  4. consider lower and upper case letters the same. (i.e. is NOT case sensitive)
  5. have one function bool Palindrome (string sText) that tests a string to see if it is a palindrome.
  6. The Palindrome function will not modify the original string input by the user

Steps to completing the assignment

Use the sample input as a guide. First, get the program to run with words that are all lower case, like rotator. Then try multiple words like nurses run. Then add the ability to handle capitals, spaces and punctuation.

getline()

Input using cin is delimited by white space. This makes it impossible to get more than one word in an string using cin. Instead, use getline. For example, the following will get a line of input from the standard input device and store it in sString:
getline(cin, sString);

accessing by index

Individual characters in a string can be accessed with an index, a number in square brackets. Like most things in C++, strings are zero-based, meaning the first character is at position zero.

string sString = "hello";
char cChar = sString[0];
//stores the letter h in cChar

length()

It is very helpful to know the length of a string. The length() function will return the total number of characters (including spaces) in the string. For example:

string sString = "Kenny Roberts Jr.";
cout<<sString.length();
//displays 17

bool Palindrome (string sText)

Your program should use a function to test whether a given string is a Palindrome. If the string is a palindrome, the function should return true otherwise it should return false. You can use the function as part of an if statement:

if(Palindrome(sString))
	cout<<sString<<" is a Palindrome"; 

#include <ctype.h>

The ctype.h header file declares useful functions for determining the type of the character. The functions isalpha() and isdigit() will tell you if a character is a letter or a number. For this assignment you will find isalnum() which returns true if a character is a letter or a digit, and toupper() or tolower() which return the corresponding upper and lower case for letters (and leaves other characters unchanged) helpful.