/******************************************************************* Palindrome.cc Author: Dana Vrsjitoru September 2020 Function that inputs a line from the user and decides whether it represents a palindrome or not. Examples of palindromes: kaya, civic, mom, cc, h, Anna, deed, A man, a plan, a canal, Panama! , step on no pets *******************************************************************/ #include using namespace std; #include #include // converts all the letters in the string to lowercase void to_lowercase(char *text) { if (! text) return; for (int i = 0; text[i] != '\0'; i++) if (isupper(text[i])) // convert all uppercase letters text[i] = tolower(text[i]); // to lowercase } // copies the source to the destination by stripping all the // characters that aren't letters void strip_not_letters(char *source, char *dest) { int i, j = 0; if (!source) return; for (int i = 0; source[i] != '\0'; i++) if (isalpha(source[i])) // copy only the letters into dest { dest[j] = source[i]; j++; } dest[j] = '\0'; // finish the dest string properly } // checks if the text is a palindrome bool is_palindrome(char *text) { if (! text) return true; // clean up the text to_lowercase(text); int len = strlen(text); char *copy = new char[len+1]; strip_not_letters(text, copy); // check for matching characters from the beginning and the end len = strlen(copy); for (int i = 0; i < len/2; i++) if (copy[i] != copy[len - i - 1]) return false; return true; } int main() { char text[50]; // hopefully it's big enough cout << "Enter a line to check if it's a palindrome" << endl; cin.getline(text, 50); if (is_palindrome(text)) cout << "Yes, it's a palindrome" << endl; else cout << "No, it's not a palindrome" << endl; }