输入一个只含大写字母和数字的字符串,判断这个字符串的性质:
1只是镜像
2只是回文
3既是镜像又是回文
4两个都不是
回文:正着读反着读都一样
镜像:
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
9
源代码:
#include <iostream>
#include<string>
using namespace std;
char* a="A 3 HIL JM O 2TUVWXY51SE Z 8 ";
string c[4]={"is not a palindrome","is a regular palindrome ","is a mirrored string","is a mirrored palindrome"};
char r(char hh)
{
if(isalpha(hh))
return a[hh-'A'];
else
return a[hh-'0'+25];
}
int main()
{
string b;
cin>>b;
int len=b.size();
int x=1,y=1;
for(int i=0;i<=((len+1)/2)-1;i++)
{
if(b[i]!=b[len-1-i])
x=0;
if(r(b[i])!=b[len-1-i])
y=0;
}
if(x==0&&y==0)
cout<<c[0]<<endl;
else if(x==1&&y==0)
cout<<c[1]<<endl;
else if(x==0&&y==1)
cout<<c[2]<<endl;
else
cout<<c[3]<<endl;
return 0;
}
知识点:
1
char a;
isalpha(a); 头文件:#include<ctype.h>
用于判断a是否为英文字母,如果a是英文字母返回正整数,不是返回0
2
两个字符型字母(大写或小写),两个字符型整数相减,结果为数字。
'5'-'1'=5
'Z'-'A'=25