欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

回文词(Palindromes,UVa401)

程序员文章站 2022-03-04 22:54:40
...

回文词(Palindromes,UVa401)

#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];
}