Recursion: base case returning 1, function returning 0
Hey,
I'm writing a recrusive function that is supposed to test a palindrome. I'm testing it out by inputing "racecar". My test statements indicate to me that the function should be returning 1(meaning it is a palindrome). However I'm getting returned 0. My understanding of recursion is a bit lacking right now I have a feeling I'm not fully understanding how a parent function handles the returned value. Could someone help me please?
Code:
#include <stdio.h>
int main()
{
char string[ 15 ];
scanf("%s", &string);
printf("%d\n", testPalindrome(string, 6, 7) );
return 0;
}
int testPalindrome(char array[], int n, int size)
{
if (size % 2 == 1 && (size - 1) / 2 == n ) {
// TEST STATEMENT
printf("if 1\n");
return 1;
}
else if (size % 2 == 0 && (size - 1) / 2 == n) {
// TEST STATEMENT
printf("if 2\n");
if (array[ n ] == array[ size - n ])
return 1;
else
return 0;
}
else {
// TEST STATEMENT
printf("if 3\n");
if (array[ n ] == array[ size - n - 1 ])
testPalindrome(array, n - 1, size);
else
return 0;
}
}