#include #include #include using namespace std; void swap(char& v1, char& v2); //switches values of v1 and v2 string reverse(const string& s); //returns string s in reverse order string remove_punct(const string& s, const string& punct); //returns string s with punctuation removed string make_lower(const string& s); //returns copy of string s with all of the characters lowercase bool is_pal(const string& s); //returns true of s is a palindrome, otherwise returns false int main() { char runagain; do{ string str; cout << "Enter a candidate for palindrome testing: "; getline(cin, str); if(is_pal(str)) cout << "\"" << str + "\" is a palindrome."; else cout << "\"" << str + "\" is not a palindrome."; cout << endl; cout << "Run the program again? (y/n): "; cin >> runagain; cin.ignore(); cout << endl; } while((runagain == 'y') || (runagain == 'Y')); return 0; }//end main void swap(char& v1, char& v2) { char temp = v1; v1 = v2; v2 = temp; }//end swap string reverse(const string& s) { int start = 0; int end = s.length(); string temp(s); while(start < end) { end--; swap(temp[start], temp[end]); start++; } return temp; }//end reverse //uses functions make_lower, remove_punct bool is_pal(const string& s) { string::const_iterator sForward = s.begin(); string::const_reverse_iterator sBackward = s.rbegin(); bool palindrome = true; for(; sForward < s.end(), sBackward < s.rend();) { if(!isalpha(*sForward)) { sForward++; continue; } if(!isalpha(*sBackward)) { sBackward++; continue; } if(tolower(*sForward) != tolower(*sBackward)) palindrome = false; sBackward++; sForward++; } return palindrome; }