• 回文串和镜像串(摘)


    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    const char* rev="A   3  HIL JM O   2TUVWXY51SE Z  8 ";      //前26项为各个大写字母的镜像,后10个是数字1至9的镜像(不输入数字0)
    const char* msg[]={"not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome"};
    char r(char ch)                     //返回字符ch的镜像字符
    {
        if(isalpha(ch))return rev[ch-'A'];        //调用函数isalpha来判断字符ch是否为字母,在ctype.h中定义(类似的还有idigit,isprint等)
        return rev[ch-'0'+25];                   //前面是26个英文字母
    }
    int main()
    {
        char s[30];
        while(scanf("%s",s)==1){
            int len=strlen(s);
            int p=1,m=1;
            for(int i=0;i<(len+1)/2;i++){
                if(s[i]!=s[len-1-i])p=0;      //不是回文串
                if(r(s[i])!=s[len-1-i])m=0;    //不是镜像串
            }
            printf("%s -- is  %s.
    
    ",s,msg[m*2+p]);
        }
        system("pause");
        return 0; 
    }

    英文不好!!

    a regular palindrome        普通回文  

    a mirrored string              镜像字符串

    a mirrored palindrome      镜像回文

    样例输出

    PS:头文件ctype.h中包含的isalpha、isdigit、isprint等工具可以用来判断字符的属性,而toupper、tolower等工具可以用来转换大小写。

  • 相关阅读:
    Python中匿名函数的应用
    Python中界面阻塞情况的解决方案
    Python中的协程,gevent模块
    Python中的进程和线程
    Python中的正则表达式用法
    Jquery瀑布流效果(下篇)
    安卓不支持keypress事件
    让MAC OS也能使用LL LA L等LS的别名
    git 常用命令
    javascript中的apply与call
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5189493.html
Copyright © 2020-2023  润新知