此题为紫书程序3-7回文词
输入一个字符串,判断它是否为回文词及镜像串。输入字符串保证不含有数字字符0。每个字符的镜像如下表:
character | reverse |
---|---|
A | A |
B | |
C | |
D | |
E | 3 |
F | |
G | |
H | H |
I | I |
J | L |
K | |
L | J |
M | M |
N | |
O | O |
P | |
Q | |
R | |
S | 2 |
T | T |
U | U |
V | V |
W | W |
X | X |
Y | Y |
Z | 5 |
1 | 1 |
2 | S |
3 | E |
4 | |
5 | Z |
6 | |
7 | |
8 | 8 |
9 |
输入的每行包含一个字符串(保证不含有空白字符且只有上表所示的字符),判断它是否为回文词和镜像串(共有4种组合)。每组数据之后输出一个空行。
样例输入
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
样例输出
NOTAPALINDROME -- is not a palindrome.
ISAPALINILAPASI -- is a regular palindrome.
2A3MEAS -- is a mirrored string.
ATOYOTA -- is a mirrored palindrome.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctype.h>
using namespace std;
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'];//isalpha用来判断字符是否为字母,类似的还有idigit,isprint等
return rev[ch-'0'+25];//注意是25而不是26
//如果ch是大写字母,则ch-'A'就是它在字母表中的序号(A的序号为1,B的序号为2,依次类推
//如果ch是数字,则ch-'0'就是这个数字的数值本身(例如'5'-'0'=5)‘
}
int main()
{
char s[33];
while(scanf("%s",s)==1)
{
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",s,msg[m*2+p]);
}
return 0;
}