//////////////////////////////////////////////////////////////////////////////// // Author: Julie Petrusa (991339) // // Due date: February 12, 2002 // // This program inputs a string of characters. Capitalization, punctuation, // // and spacing are ignored. The function Palindrome returns true if the // // string is a palindrome, and false if it is not. A palindrome is a word or // // a phrase whose letters read the same forward or backward. // // Known bugs: none // //////////////////////////////////////////////////////////////////////////////// #include // needed in order to do I/O #include // for system("PAUSE") #include // for islower(), isupper(), tolower() bool palindrome(char text[]) { int length = strlen(text); // get length of string int front = 0; // initialize counter for front half int rear = length-1; // initialize counter for rear half while (front < rear) // stop when front has checked half the string { while (!islower(text[front])) // while not small letter { // in front half if (isupper(text[front])) // if capital letter text[front] = tolower(text[front]); // change to small letter else // else ignore punctuation front++; } while (!islower(text[rear])) // while not small letter { // in rear half if (isupper(text[rear])) // if capital letter text[rear] = tolower(text[rear]); // change to small letter else // else ignore punctuation rear--; } if (text[front] != text[rear]) // if front is not equal to rear return false; // exit function returning false front++; // increment front rear--; // decrement rear } return true; // exit function returning true } int main() { cout << "\n\nAuthor: Julie Petrusa (991339)\n"; // display banner cout << "Due date: February 12, 2002\n"; cout << "This program inputs a string of characters. Capitalization, "; cout << "punctuation, and\nspacing are ignored. The function Palindrome "; cout << "returns true if the string is a\npalindrome, and false if it is "; cout << "not. A palindrome is a word or a phrase whose\nletters read the "; cout << "same forward or backward.\n"; cout << "\n\nChoose one of the following options:\n"; // display options cout << " 1 --> Enter a palindrome\n"; cout << " 2 --> Exit the program\n"; cout << "Answer: "; int answer; cin >> answer; switch (answer) { case 1: cout << "\n\nPalindrome: "; char text[100]; cin.setf(ios::skipws); // ignore spacing cin.ignore(2, '\n'); cin.get(text, 100); if (palindrome(text)) cout << "\n\nThat is a palindrome!\n"; else cout << "\n\nThat is NOT a palindrome!\n"; break; } cout << "\n\n"; system("PAUSE"); // terminate return 0; } // end Palindromes