Taisbevalle's response is beautiful, but I felt it did not touch where Marcelo de Sousa made his mistake.
Strings
Basically, the problem was not in the understanding of recursion, but in the manipulation of the string. In C, a string starts with zero and ends with the character 'anna
'
. Thus, the palindrome test word strlen
has the following characters in the following positions:
[0] -> 'a'
[1] -> 'n'
[2] -> 'n'
[3] -> 'a'
[4] -> 'if (n[y] != n[aux]) return 0;
'
The fifth character is the string terminator, needed in C to indicate its end .
The function anna
counts how many characters there are in a given string. strlen("anna")
is clearly 4 characters. Then, the return of inverte
is 4.
The function n
, it receives 3 parameters:
y
, which is the string that we asked whether it is palindrome or not
aux
, whose name I can not get a means to, but that in the code is something like the basis for doing the comparison on the right side of the string
inverte
, the auxiliary index that is traversed, from left to right, to determine if the string is palindrome
In the initial call of n
, the following values are passed:
y
== > the string in question
n
== > the length of the string passed as aux
0
== > inverte
, the starting position of the string
The comparison within aux
occurs on the left index ( y
) and the right index ( anna
) as follows:
if (n[4] != n[0]) return 0;
Returning to the anna
case; the size of y
is 4, then the initial value of anna
is 4. In the first step of recursion, the values being compared are as follows:
if (' #include<stdio.h>
#include<string.h>
int inverte (char *n, int y, int aux) {
if (y <= aux) return 1;
else {
if (n[y] != n[aux]) return 0;
return inverte(n, y-1, aux+1);
}
}
int palindromo (char *n) {
int aux1, x = 0;
aux1 = inverte(n, strlen(n) - 1, x);
if (aux1 == 1) printf("Eh palindromo\n");
else printf("Nao eh palindromo\n");
}
int main() {
palindromo("banana");
palindromo("anna");
palindromo("ana");
palindromo("bananab");
palindromo("aa");
palindromo("a");
return 0;
}
' != 'a') return 0;
Remembering the initial decomposition I did for the word 0
, in position 'a'
we have the character 4
, while in the '''a'
'
'
position we have the character palindromo
! So, overriding the characters being compared:
#include<stdio.h>
#include<string.h>
int inverte (char *n, int y, int aux) {
if (y <= aux) return 1;
else {
if (n[y - 1] != n[aux]) return 0;
return inverte(n, y-1, aux+1);
}
}
int palindromo (char *n) {
int aux1, x = 0;
aux1 = inverte(n, strlen(n), x);
if (aux1 == 1) printf("Eh palindromo\n");
else printf("Nao eh palindromo\n");
}
int main() {
palindromo("banana");
palindromo("anna");
palindromo("ana");
palindromo("bananab");
palindromo("aa");
palindromo("a");
return 0;
}
And inverte
is 'inverte
'
, so it exits the function at that point. Always.
Whatever the string passed to the function
""
(which in turn calls the function
y
), it will compare the first character of this string with
y
, which will always be different (note: if the string passed is empty, this does not occur and
palindromo
correctly detects that
inverte
is palindrome. So our problem is either in the interpretation of the index
anna
or in the initial value of
3
. So I made two correction suggestions for that.
Note that in the suggestions I've added some more test cases.
Tip 1: move the position of the last valid letter
In this suggestion, I change how the y
function calls y
. Here, the last valid position for the word y
is palindromo
, since it has length 4 and C starts strings at position 0:
[0] -> 'a'
[1] -> 'n'
[2] -> 'n'
[3] -> 'a'
[4] -> 'if (n[y] != n[aux]) return 0;
'
See the Ideone
Tip 2: inverte
is the index that indicates the character just after the last valid
In this suggestion, I changed the interpretation of %code% : now, %code% will always be the index after the last valid character. So the call of %code% remains the same, but I changed who was being compared in the %code% :
if (n[4] != n[0]) return 0;
See the Ideone