recursive palindrome string

Hi, I am working on a recursive palindrom program. All of my cases seem to work except for the case that a word has the same first and last letters but is NOT a plaindrome. It seems that at random, the program will some times provide the correct output. I think the problem is that the recursive call is not actually checking anything past the first and last character in the word, but I am not sure why?
Thanks!
Last edited on
1
2
3
4
5
6
else if ((word[0]) == (word[len-1]))
{
return true;
word = word.substr(1,len-2);
isPalindrome(word);
}


The above piece of doesn't check if a substring word.substr(1,len-2) is a palindrome, it always returns true (after calling return you leave the function). Also, the condition always holds.

You can rewrite this in the following way:
1
2
3
else{
return isPalindrome(word.substr(1, len-2));
}
Thanks that makes sense!
Topic archived. No new replies allowed.