#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; 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 and string make_lower(const string& s) { string temp(s); for (int i = 0; i < s.length(); i++) temp[i] = tolower(s[i]); return temp; }//end make_lower string remove_punct(const string& s, const string& punct) { string no_punct; //initialized to empty string int s_length = s.length(); int punct_length = punct.length(); for (int i = 0; i < s_length; i++) { string a_char = s.substr(i,1); // a 1 char string int location = punct.find(a_char, 0); //find location of successive characters of src in punct_length if ((location < 0) || (location >= punct_length)) no_punct = no_punct + a_char; //a_char not in punct, so keep it } return no_punct; } //uses functions make_lower, remove_punct bool is_pal(const string& s) { string punct(",;:.?!'\" "); //includes a blank string str(s); str = make_lower(str); string lower_str = remove_punct(str, punct); return (lower_str == reverse(lower_str)); }