segmentation fault [closed]
#include <stdio.h>
#include <string.h>
int test(char ch [10],int i,int j )
{
if(i>=j) return 1;
else if (ch[i]!=ch[j开发者_Python百科]) return 0;
else return (test(ch,i++,j--));
}
int main ()
{
char ch[10];
int m,k;
printf("Donner une chaine de caracteres :\n");
scanf("%s",ch);
k=strlen(ch);
m=test(ch,0,k-1);
if (m==1) printf ("expression palindrome \n");
else printf ("expression non palindrome \n");
return 0;
}
Try replacing this:
else return (test(ch,i++,j--));
...with this:
else return (test(ch,i+1,j-1));
There's no need to assign back into 'i' and 'j' when making that call, since you don't reference them again in the same function invocation. Moreover, i++
evaluates to the original value of i
, and not the value of i + 1
(which is what you want here).
So your original code would never actually modify i
and j
, which would cause it to recurse infitely and cause a stack overflow (so I can't believe people were saying that this wasn't appropriate for SO).
精彩评论