// // palindrome testing programs // // Described in Chapter 7 of // Data Structures in C++ using the STL // Published by Addison-Wesley, 1997 // Written by Tim Budd, budd@cs.orst.edu // Oregon State University // # include # include # include bool isPalindrome_type1 (string & aString) // test to see if aString is an exact word palindrome { string temp; // declare temporary temp = aString; // duplicate argument reverse (temp.begin(), temp.end()); // reverse duplicate return temp == aString; // test for equality } bool isPalindrome_type2 (string & aString) // test to see if aString is a case insensitive palindrome { string allLow (aString); transform (aString.begin(), aString.end(), allLow.begin(), tolower); return isPalindrome_type1 (allLow); } string remove_all (string & text, string & spaces) // remove all instances of characters from spaces { string result; int textLen = text.length(); int spacesLen = spaces.length(); for (int i = 0; i < textLen; i++) { string aChar = text.substr(i, 1); int location = spaces.find(aChar, 0); if ((location < 0) || (location >= spacesLen)) result += aChar; } return result; } bool isPalindrome_type3 (string & aString) // see if text is a punctuation insenstive palendrome { // remove all punctuation and space characters string punctChars = " ,.!?"; string temp = remove_all (aString, punctChars); // then test resulting string return isPalindrome_type2 (temp); } void pal_test (string & aString) // test the argument string to see if it is a palindrome { if (isPalindrome_type1(aString)) cout << "is a type 1 palindrome\n"; else if (isPalindrome_type2(aString)) cout << "is a type 2 palindrome\n"; else if (isPalindrome_type3(aString)) cout << "is a type 3 palindrome\n"; else cout << "is not a palindrome\n"; } void main () { string aString; cout << "enter a palendrome:\n"; while (getline(cin, aString)) { pal_test(aString); cout << "enter a palendrome:\n"; } }