#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]) 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;
}
最佳答案
尝试替换这个:
else return (test(ch,i++,j--));
...这样:
else return (test(ch,i+1,j-1));
进行该调用时无需重新分配给“i”和“j”,因为您不会在同一函数调用中再次引用它们。此外,i++
的计算结果为 i
的原始值,而不是 i + 1
的值(这正是您想要的)。
所以你的原始代码永远不会真正修改 i
和 j
,这会导致它无限递归并导致堆栈溢出(所以我不敢相信人们是说这不适合 SO)。
关于c - 分段故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5441970/