01: #include <iostream>
02: #include <string>
03: using namespace std;
04: /**
05: Tests whether a string is a palindrome. A palindrome
06: is equal to its reverse, for example "rotor" or "racecar".
07: @param s a string
08: @return true if s is a palindrome
09: */
10: bool is_palindrome(string s)
11: { // separate case for shortest strings
12: if (s.length() <= 1) return true;
13:
14: // get first and last character, converted to lowercase
15: char first = s[0];
16: char last = s[s.length() - 1];
17:
18: if (first == last)
19: { string shorter = s.substr(1, s.length() - 2);
20: return is_palindrome(shorter);
21: }
22: else return false;
23: }
24:
25: int main()
26: { cout << "Enter a string: ";
27: string input;
28: getline(cin, input);
29: cout << input << " is ";
30: if (!is_palindrome(input)) cout << "not ";
31: cout << "a palindrome" << endl;
32: return 0;
33: }