Tests whether a string is a palindrome
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool is_palindrome(string s)
{
if (s.length() <= 1)
return true;
char first = s[0];
char last = s[s.length() - 1];
if (first == last){
string subString = s.substr(1, s.length() - 2);
return is_palindrome(subString);
}
else
return false;
}
int main()
{
cout << "Enter a string: ";
string input;
getline(cin, input);
if (!is_palindrome(input))
cout << "false";
return 0;
}
Related examples in the same category