Hey guys,
I'm currently working on a function which checks whether the string argument is a palindrome (word which remains the same if it's read backwards) or not.
I wanted to achieve this by using a recursive call.
Long story short, here's my code:
#include <string>
#include <iostream>
usingnamespace std;
bool palindrome(string word) {
int i = 0;
int j = word.size()-1;
if (i >= j) returntrue; // end of recursion
if (word[i] != word[j])
returnfalse;
word.erase(word.begin());
word.erase(word.end()-1);
palindrome(word); // recursive call
}
int main() {
bool b = palindrome("ada");
if (b)
cout << "Palindrom!\n";
else
cout << "Not a palindrom\n";
return 0;
}
It always gets me "Not a palindrom", but can't figure out the mistake.
Thanks in advance :)