首页 > 代码库 > 回文词

回文词

#include <iostream>#include <stdio.h>#include <iomanip>#include <string>using namespace std;const char *rev = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";//字符A-9的镜像字符,没有镜像则为空格const char * msg[] = {"not a palindrome","a regular palindrome","a mirrored string","a mirrired palindrome"};//输出字符串数组bool isPalindromes(char s[]) //判断是否为回文字符串{    long len = strlen(s);    for(int i = 0;i<len/2;i++)    {        if(s[i] != s[len - 1 - i])            return false;    }    return true;}char r(char ch) //获得字符的镜像字符{    if(isalpha(ch)) return rev[ch - A];    return rev[ch - 0 + 25];}bool isMirror(char s[])//判断字符串是否为镜像字符串{    long len = strlen(s);    for(int i = 0; i<len/2;i++)    {        if(r(s[i]) != s[len - i -1])            return false;    }    return true;}int main(){    char s[30];    while(scanf("%s",s) == 1)    {        int flag1 = 1, flag2 = 1;        if(isPalindromes(s) == false) flag1 = 0;        if(isMirror(s) == false) flag2 = 0;        printf("%s -- %s\n",s,msg[2*flag2+flag1]);    }    return 0;}

 说明:

回文词:Palindromes 是指一个序列从正向遍历和逆向遍历得到的结果一样。比如ISABTCTBASI

镜像:Mirror是指一个序列按照中间元素左右对称。比如ESTAT23