#include <iostream>
#include <string>
using namespace std;
/*
Recursive function to find palindrome
@input string to test if it's a palindrome
@return true if string is a palindrome, false otherwise
*/
bool is_palindrome(string str)
{
// base case
if (str.length() <= 1)
{
return true;
}
// if first and last letter are the same
if (str.substr(0, 1) != str.substr(str.length() - 1))
{
return false;
}
else{
// and the rest of me is a palindrome
return is_palindrome(str.substr(1, str.length() - 2));
}
}
int main()
{
cout << "Word: ";
string input;
getline(cin,input);
if (is_palindrome(input))
{
cout << "IT'S A PALINDROME!!!" << endl;
}
else
{
cout << "not a palindrome" << endl;
}
char ch;
cin >> ch;
return 0;
}
Back to top