#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
void main()
{
system("color f0");
char string[100];
int a,b,i,len,choice=1,flag; //flag作个标记
while(choice)
{
system("cls");
printf("请输入您想判断的回文:");
gets(string);
len=strlen(string);
len--; //因为数组的第一位为0
for(i=0;i<=len/2;){
unsigned char ch = string[i]; //取其中一个
if(ch > 0x8E && ch < 0xFF) //判断是否为中文
{
a=i;b=len-i;
/*因为汉字是2个字节,一一对应去比较*/
unsigned tchar1=string[a];
unsigned tchar2=string[a+1];
unsigned tchar3=string[b];
unsigned tchar4=string[b-1];
if(tchar1==tchar4&&tchar2==tchar3)
flag=1;
else
{
flag=0;
break;
}
i+=2; //移动2个字节
}
else
{
a=i;b=len-i;
if(string[a]==string[b])
flag=1;
else
{
flag=0;
break;
}
i++;
}
}
if(flag) printf("是回文\n");
else printf("不是回文\n");
printf("继续按1 ; 退出按0。\n");
scanf("%d",&choice);
fflush(stdin); //清除键盘缓冲区,把回车吃掉
}
}
回文检测(包含中文与字符)
最新推荐文章于 2022-04-28 22:14:36 发布