/* ************************************************************************** * Program name : 046_Palindrome_string (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/28 - first version * ************************************************************************** * Description : (a) Introduction * * (b) Asking the user to input a string * * (e) Find the length of the string * * (e) Print the string forward and backward * * (e) Is that a palindrome string ? * * (e) Try again ? * ************************************************************************** */ #include #include #include int main() { // part 1 : declaration const int Size = 16; char InputString[Size]; int Index, Length; char Pali, again; // part 2 : Introduction cout << "\n\n\t\tPalindrome String (Version 1.00)\n" << "\n\tPlease input a string (without space and less than 30" << "\n\tcharacters long), then this program will check this" << "\n\tstring is palindrome or not."; do { // part 3 : Asking the user to input a string cout << "\n\n\tYour string is : "; cin >> InputString; // part 4 : Find the length of the string for (Index=0; Index<=(Size-1); Index++) { Length = Index; if (InputString[Index]=='\0') break; } // part 5 : Print the string forward and backward. cout << "\n\tYour string [ "; for (Index=0; Index<=Length-1; Index++) cout << InputString[Index]; cout << " ] contains " << Length << " characters or digits" << "\n\n\tIn reverse order, the string becomes : "; for (Index=Length-1; Index>=0; Index--) cout << InputString[Index]; // part 6 : Is that a palindrome string ? Pali = 1; // Pali equals to 1 means the string is palindrome. for (Index=0; Index<=(Length / 2)-1; Index++) if (InputString[Index] != InputString[Length-Index-1]) { Pali = 0; // Pali equals to 0 means the string is not palindrome. break; // No futher checking if mismatch found } if (Pali ? cout << "\n\n\tYour string is a palindrome string.\n" : cout << "\n\n\tYour string is not a palindrome string.\n"); // part 6 : try another number ? cout << "\n\n\t\aTry another number (Y/N) : "; cin >> again; cout << "\n"; } while (again=='Y' || again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; }