回文检测(包含中文与字符)

#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);  //清除键盘缓冲区,把回车吃掉 
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值