//2016年/6/3日重刷
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
const string s ="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
const string s1="A 3 HIL JM O 2TUVWXY51SE Z 8 ";
int main()
{
char c;
string s2;
//freopen("in1.txt","r",stdin);
map<char,char>M;
for(int i=0;i<s.size();i++)
M[s[i]]=s1[i];
while(cin>>s2)
{
string s3=s2;
reverse(s3.begin(),s3.end());
if(s3==s2){
int flag=1;
for(int i=0;i<s2.size();i++)
if(M[s3[i]]!=s2[i]){
flag=0;
break;
}
if(flag)
cout<<s2<<" -- is a mirrored palindrome."<<endl<<endl;
else
cout<<s2<<" -- is a regular palindrome."<<endl<<endl;
}
else{
int flag=1;
for(int i=0;i<s2.size();i++)
if(M[s3[i]]!=s2[i]){
flag=0;
break;
}
if(flag)
cout<<s2<<" -- is a mirrored string."<<endl<<endl;
else
cout<<s2<<" -- is not a palindrome."<<endl<<endl;
}
}
return 0;
}
//下面是书中代码
#include<cstdio>
#include<ctype.h>
#include<cstring>
#include<fstream>
#include<iostream>
//#define LOCAL
using namespace std;
const char* s1[]={"not a palindrome.","a regular palindrome.","a mirrored string.","a mirrored palindrome."};
const char* s2="A 3 HIL JM O 2TUVWXY51SE Z 8 ";
char r(char c)
{
if(isalpha(c)) return s2[c-'A'];
else return s2[c-'0'+25];
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // LOCAL
char s[25];
while(scanf("%s",s)==1)
{
int m=1,n=1;
int len=strlen(s);
for(int i=0; i<(len+1)/2; i++)
{
if(s[i]!=s[len-i-1]) m=0;
if(r(s[i])!=s[len-i-1]) n=0;
}
printf("%s -- is %s\n\n",s,s1[2*n+m]);
}
return 0;
}
总结
①声明一个二维字符数组 char* s1[]={ "","","" };
声明一个一维字符数组 char* s2=“”;
②isalpha 在ctype.h中定义