I wrote a recursive function to find if a given string is a palindrome
Code:
#include <stdio.h>
int isPalindromeRec(char *start, char* end);
int main()
{
char str[] = "abccba";
//pointers to string
char* start = str;
char* end = str;
while(*end != '\0')
end++;
end--;
if(isPalindromeRec(start, end))
printf("String is a palindrome");
else
printf("Not a palindrome");
getchar();
return 0;
}
//recursive function to check if a string is a palindrome
int isPalindromeRec(char *start, char* end)
{
if(end<=start)
return 1;
//check if the two characters match
if(*start==*end)
{
start++;
end--;
isPalindromeRec(start, end);
}
else
return 0;
}
This code works fine.
I changed the isPalindromeRec function to
Code:
int isPalindromeRec(char *start, char* end)
{
if(end<=start)
return 1;
else
{
return(*start==*end && isPalindromeRec((start++), (end--)));
}
}
which is the same as the function above. However, it doesnt work the same way. On debugging I noticed that the start and end pointers are not incremented inside the isPalindromeRec((start++), (end--)) call.
Why does this happen?