1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
// i - index from the left
// j - index from the right
bool IsPalindrome(const string& s, int i, int j)
{
if (s.size() <= 1)
return true;
if (i >= j)
return true;
return s[i] == s[j] && IsPalindrome(s, i+1, j-1);
}
int main()
{
vector<string> examples =
{
"",
"a",
"Hello, world!",
"A man, a plan, a canal--Panama!",
"civic",
"racecar",
"rotator",
"rotor",
};
cout << boolalpha;
for (auto& e : examples)
cout << setw(33) << right << e << " : " <<
IsPalindrome(e, 0, e.size()-1) << endl;
return 0;
}
|