例题3-3 回文词(Palindromes)

来源:互联网 发布:信用卡淘宝套现技巧 编辑:程序博客网 时间:2024/05/20 14:41
#include <iostream>#include <string.h>#include <stdio.h>#include <ctype.h>const char* rev="A   3  HIL JM O   2TUVWXY51SE Z  8 ";const char* msg[]={"not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome"};char r(char ch){    if(isalpha(ch)) return rev[ch-'A'];    return rev[ch-'0'+25];}using namespace std;int main(){    char s[30];    while(scanf("%s",s)!=EOF)    {        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.\n\n",s,msg[m*2+p]);    }    return 0;}
/*注意 char* 相当于一维数组 char*[] 相当于二维数组(每个引号间的内容相当于一行 char[][]取一行中的一个字符) 此题还得参考书上的镜面表 isalpha的头文件是 ctype.h*/

0 0
原创粉丝点击