回文词(Palindromes,UVa401)
程序员文章站
2022-03-04 22:54:40
...
#include <iostream>
#include<cctype>
#include<string>
using namespace std;
char mirror(char);
const char mir[] = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
const char* msg[] = { " -- is not a palindrome."," -- is a regular palindrome.",
" -- is a mirrored string."," -- is a mirrored palindrome." };
int main()
{
string str;
while (cin >> str)
{
int p = 1, m = 1;
int size = str.size();
for (int i = 0; i < (size + 1)/ 2; ++i)//+1是为了确保奇数情况下中间的字符镜像后为合法字符
{
if (str[i] != str[size - 1 - i])
p = 0;
if (str[i] != mirror(str[size - 1 - i]))
m = 0;
}
cout << str << msg[p + m * 2] << endl << endl;
}
}
char mirror(char ch)
{
if (isalpha(ch))
{
return mir[ch - 'A'];
}
else
return mir[ch - '0' + 25];
}