输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字 0。所谓回文串,就是反转以后和原串相同,如 abba 和 madam。所有镜像串,就是左右镜像之后和原串相同,如 2S 和3ALAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。在本题中,每个字符的镜像如图 3-3 所示(空白项表示该字符镜像后不能得到一个合法字符)。
样例输入:
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 <stdio.h>
#include <string.h> //strlen()
#include <ctype.h> //isalpha,isdigit,isprint判断字符属性,toupper,tolower转换大小写
void std_ans();
char m(char c);
void test01();
const char* mir = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";//对应镜像的A-Z,1-9 共35个
const char* msg[] = { "not a palindrome","a regular palindrome","a mirrored string ","a mirrored palindrome"};//msg是一个指针数组
int main()
{
const char* (*p)[4]=&msg;
std_ans();
return 0;
}
void std_ans()
{
char s[30];
while (scanf("%s", s) == 1) {
int len = strlen(s);
int p = 1, q = 1;
for (int i = 0; i < (len + 1) / 2; i++) {
if (s[i] != s[len - 1 - i]) p = 0;//不是回文
if (m(s[i]) != m(s[i])) q = 0;//不是镜像
}
printf("%s -- is %s.\n\n", s, msg[2 * q + p]);
}
}
char m(char c) {
if (isalpha(c)) return mir[c - 'A'];
return mir[c - '0' + 25];
}