In order to check for a palindrome, your recursive function checks the first character in the string against the last character in the string, and then "discards" both of them. So the comparisons look like this:
Code:
STEP 1 STEP 2 STEP 3
abcddcba _bcddcb_ __cddc__
^ ^ ^ ^ ^ ^
You accomplished "discarding" the first character by incrementing your string pointer by 1 when you passed the string to the recursive call. And to "discard" the last character you adjusted the length value when you passed it to the recursive call.
Unfortunately, since you modified the function to use strlen(), you are recalculating the length each recursive call and not adjusting it to discard the characters at the end that you've already checked. Basically, you're doing this:
Code:
STEP 1 STEP 2 STEP 3
abcddcba _bcddcba __cddcba
^ ^ ^ ^ ^ ^
(of course this would actually fail at STEP 2)
To handle situations like these, I usually write 2 functions. One has a very simple signature that is easy to use, and the other is the recursive function that does all the work and has a more complicated signature. In your case:
Code:
int isPalindrome_r(char *str, int length) {
if (length<1)
return 1;
if (str[0] == str[length-1])
return isPalindrome (str+1, length-2);
else
return 0;
}
int isPalindrome(char *str) {
return isPalindrome_r(str, strlen(str));
}