就是利用常量数组来判断是否镜像,代码如下。
#include<stdio.h>
#include<iostream>#include<string.h>
#include<ctype.h>
using namespace std;
char rStr[100]={'A',' ',' ',' ','3',' ',' ','H','I','L',' ','J','M',' ','O',' ',' ',' ','2','T','U','V','W','X','Y','5','1','S','E',' ','Z',' ',' ','8',' '};
string msg[100]={"is not a palindrome.","is a regular palindrome.","is a mirrored string.","is a mirrored palindrome."};
char r(char ch)
{
if(isdigit(ch))
return rStr[ch-'0'+25];
if(isalpha(ch))
return rStr[ch-'A'];
}
int main()
{
string str;
while(cin>>str)
{
int len=str.length();
int flag1=1,flag2=2;
for(int i=0;i<len/2+1;i++)
{
if(str[i]!=str[len-i-1])flag1=0;
if(str[i]!=r(str[len-i-1]))flag2=0;
}
cout<<str<<" -- "<<msg[flag1+flag2]<<endl<<endl;
}
}