UVa401

#include<iostream>
#include<stdlib.h>
#include<string.h>

using namespace std;

const char *rev = "A   3  HIL JH O   2TUVWXXY51SE Z  8 ";
const char *msg[] = { "not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome" };

char r(char ch) {
	if (isalpha(ch)) return rev[ch - 'A'];
	else return rev[ch - '0' + 25];
}
int main(void) {
	char s[30];
	while (scanf("%s", s) != EOF) {
		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 - i - 1]) m = 0;
		}
		cout << s << " -- is " << msg[m * 2 + p] << endl << endl;
	}
	system("pause");
	return 0;
}

.首先判断一下ch字符是数字还是字母,然后返回的值是ch的镜像字符,返回ch-‘A’相当于这个字符在rev中的序号(A的序号是0,B的序号是2.……)

msg相当于一个二维字符数组,根据p和m的取值,输出相应的文字信息。

猜你喜欢

转载自blog.csdn.net/qq_40458825/article/details/79464447