"回文"是指顺读和反读内容都相同的字符串,如:"ABCCBA".这里引入两个指针变量,开始时,
分别指向字符串的首末字符,当两个指针所指字符相等时,两指针分别向前向后移一个字符位置,
并继续比较,直到两指针相遇.则说明该字符串是回文.若比较过程中,发现两字符不相等,则
可以判断该字符串不是回文.
代码如下:
/*判断字符串是否是回文*/
#include<stdio.h>
#define MAX 50
int cycle(char *s)
{
char *h,*t;
for(h=s,t=s+strlen(s)-1;t>h;h++,t--)
{
if(*h!=*t)
{
printf("%c",h);
break;
}
}
return t<=h;
}
main()
{
char s[MAX];
while(1)
{
puts("Please input the string you want to judge(input ^ to quit):");
scanf("%s",s);
if(s[0]=='^')
break;
if(cycle(s))
printf("%s is a cycle string.\n",s);
else
printf("%s is not a cycle string.\n",s);
}
puts("\nThank you for you using ,bye bye!\n");
}
输出结果:
Please input the string you want to judge(input ^ to quit):
abcabc
abcabc is not a cycle string.
Please input the string you want to judge(input ^ to quit):
abccba
abccba is a cycle string.
Please input the string you want to judge(input ^ to quit):
posted on 2006-11-05 16:58
matthew 阅读(4128)
评论(1) 编辑 收藏 所属分类:
数据结构与算法设计