#include <stdio.h>
#include <string.h>
#include <ctype.h>
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"};
//常量指针,指向的常量不能变
//指针常量,int *const b = &a;指针是一个常量,指针不变
char r(char ch)
{
if(isalpha(ch)) //isalpha(),是否为英文,大写返回2,小写返回1,不是返回0
return rev[ch - 'A']; //单个字符单引号
else
return rev[ch - '0' + 25];
}
int main()
{
char s[30];
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-i-1]) p = 0;
if(r(s[i]) != s[len-1-i]) m = 0;
}
printf("%s -- is %s.\n\n", s, msg[m*2+p]); //注意!巧妙
}
return 0;
}
结果:
NOTAPALINDROME
NOTAPALINDROME -- is not a palindrome.
ISAPALINILAPASI
ISAPALINILAPASI -- is a regular palindrome.
2A3MEAS
2A3MEAS -- is a mirrored string.
ATOYOTA
ATOYOTA -- is a mirrored palindrome.
输入一个字符串,判断它是否为回文串(abba)以及镜像串(3AIAE)。
Character | Reverse | Character | Reverse | Character | Reverse |
A | A | M | M | Y | Y |
B | N | Z | 5 | ||
C | O | O | 1 | 1 | |
D | P | 2 | S | ||
E | 3 | Q | 3 | E | |
F | R | 4 | |||
G | S | 2 | 5 | Z | |
H | H | T | T | 6 | |
I | I | U | U | 7 | |
J | L | V | V | 8 | 8 |
K | W | W | 9 | ||
L | J | X | X |