A palindrome is a phrase that reads the same forward and backward (examples: ‘racecar’, ‘radar’, ‘noon’, or ‘rats live on no evil star’). By extension we call every string a palindrome that reads the same from left to right and from right to left. Develop a recursive algorithm that takes as input a string and decides whether the string is a palindrome. Implement your algorithm in the PalindromeChecker(String) method.
Asked
Active
Viewed 608 times
-1
-
1Does this answer your question? [How do I check if a number is a palindrome?](https://stackoverflow.com/questions/199184/how-do-i-check-if-a-number-is-a-palindrome) – Amal K Jan 03 '21 at 11:18
1 Answers
0
#include <iostream>
#include <string>
using namespace std;
bool isPolindrom(string s){
int len = s.length();
if (len < 2) return true; // if s == '' or s == <one symbol>
bool condition = (s[0] == s[len-1]); // first symbol == last symbol
return condition && isPolindrom(s.substr(1, len-2)); // recursion
}
int main()
{
string name;
cout << "What is your word? ";
getline (cin, word);
cout << "it is" << (isPolindrom(word) ? " polindrom" : " not polindrom");
}

Daniil Loban
- 4,165
- 1
- 14
- 20