本题要求编写函数,判断给定的一串字符是否为“回文”。
所谓“回文”是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是回文。
函数接口定义:
bool palindrome( char *s );
函数palindrome判断输入字符串char *s是否为回文。若是则返回true,否则返回false。
裁判测试程序样例:
#include <stdio.h>
#include <string.h>
#define MAXN 20
typedef enum {false, true} bool;
bool palindrome( char *s );
int main()
{
char s[MAXN];
scanf("%s", s);
if ( palindrome(s)==true )
printf("Yes\n");
else
printf("No\n");
printf("%s\n", s);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例1:
thisistrueurtsisiht
输出样例1:
Yes
thisistrueurtsisiht
输入样例2:
thisisnottrue
输出样例2:
No
thisisnottrue
bool palindrome( char *s )
{
int i,len,j;
len=strlen(s);
for(i=0,j=len-1;i<j;i++,j--)//使用单个循环从两头往中间走
{
if(s[i]!=s[j])
return 0;
}
return 1;
}
该博客介绍如何编写一个函数,用于判断输入的字符字符串是否为回文。回文是指正读反读均相同的字符串。文章提供了一个函数接口定义及裁判测试程序样例,展示如何检查一个字符串是否为回文,并给出了不同输入的预期输出结果。

527

被折叠的 条评论
为什么被折叠?



