#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