6-5 判断回文字符串 (20分)
本题要求编写函数,判断给定的一串字符是否为“回文”。所谓“回文”是指顺读和倒读都一样的字符串。如“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
(20分)
作者
C课程组
单位
浙江大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
bool palindrome( char *s )
{
int count=0,i=0;
/* 如果不用strlen函数
int *cun=s;
while(*s!='\0'){
count++;
s++;
}
s=cun 经过上面循环后,指针不再只想s基地址,然后下面s[0]也不是指基地址,所以先用变量保存
*/
count=strlen(s);
count--;
int ret=1;
while(i<count){
if(s[i]!=s[count]){
ret=0;
break;
}
i++;
count--;
}
if(ret==1) return true;
else return false;
}