C语言:编写一个测试一个串是否为回文的递归函数,是回文,返回1;不是,返回0。
答案:4 悬赏:20 手机版
解决时间 2021-03-02 22:54
- 提问者网友:原来太熟悉了会陌生
- 2021-03-02 13:40
C语言:编写一个测试一个串是否为回文的递归函数,是回文,返回1;不是,返回0。
最佳答案
- 五星知识达人网友:千夜
- 2021-03-02 15:00
#include
int huiwen(char *s,int n)
{if(n<2)return 1;
if(*s!=*(s+n-1))return 0;
return huiwen(s+1,n-2);
}
int main()
{char s[200];
int i,k;
scanf("%d%*c",&k);
while(k--)
{gets(s);
for(i=0;s[i];i++);
printf("%s
",huiwen(s,i)?"Yes!":"No!"); //运行完了才加上的"!"
}
return 0;
}
int huiwen(char *s,int n)
{if(n<2)return 1;
if(*s!=*(s+n-1))return 0;
return huiwen(s+1,n-2);
}
int main()
{char s[200];
int i,k;
scanf("%d%*c",&k);
while(k--)
{gets(s);
for(i=0;s[i];i++);
printf("%s
",huiwen(s,i)?"Yes!":"No!"); //运行完了才加上的"!"
}
return 0;
}
全部回答
- 1楼网友:爱难随人意
- 2021-03-02 16:07
#include
#include
int check( char *str, int len )
{
if ( len > 1 )
{
if ( *str!=*(str+len-1) )
{
return 0;
}
return check( str+1, len-2 );
}
else
return 1;
}
void main()
{
int rep=0;
char str[100];
scanf("%d", &rep); getchar();
while( rep-- )
{
gets(str);
if ( check(str,strlen(str) ) == 1 )
printf("Yes! ");
else
printf("No! ");
}
}
#include
int check( char *str, int len )
{
if ( len > 1 )
{
if ( *str!=*(str+len-1) )
{
return 0;
}
return check( str+1, len-2 );
}
else
return 1;
}
void main()
{
int rep=0;
char str[100];
scanf("%d", &rep); getchar();
while( rep-- )
{
gets(str);
if ( check(str,strlen(str) ) == 1 )
printf("Yes! ");
else
printf("No! ");
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯