// functions tesing for palindrome // Walt Savitch // 03/09/00 #include "palindrome.h" // returns true if string is palindrome bool isPal(const string& tocheck){ const string punctuation(",;:.?!'\" "); //punctuation to remove string str = removePunct(tocheck, punctuation); str = makeLower(str); return (str == reverse(str)); } //returns a copy of src with characters in punct removed string removePunct(const string& src, const string& punct){ string tmp(src); int src_len = src.length(); // loop through string until all // punctuation symbols removed int location = tmp.find_first_of(punct); while(location != string::npos){ tmp.erase(location,1); location = tmp.find_first_of(punct); } return tmp; } // lowers the case of string, returns lower case string makeLower(const string& s) { string tmp; int slength=s.length(); char c; for (int i = 0; i < slength; i++) tmp += tolower(s[i]); return tmp; } // reverses the string, retruns reversed string reverse(const string& str){ string tmp(str); int start = 0; int end = tmp.length()-1; for( ;start < end; start++, end--) swap(tmp[start], tmp[end]); return tmp; } // swaps the characters void swap(char& lhs, char& rhs) { char tmp = lhs; lhs = rhs; rhs = tmp; }