判断是否为回文串或者(和)镜像串
题目描述:输入一个字符串,判断他是否为回文串或者镜像串。输入数字保证不含0。补充:镜像串就是翻转过来相同的,比如S和2。 (啊,是书上来的,代码也是按照书上的思路写的来着。)
代码: 是c++ 的代码啊
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
const char* rev = "A 3 HIL JM O9 2TUVWXY51SE Z 8P0";
const char* msg[] = { "哪个都不是","是回文数","是镜像数","两者都是" };
char r(char ch) {
if (isalpha(ch)) return rev[ch - 'A'];
return rev[ch - '0' + 25];
}
int main() {
cout << "请输入一个字符串:" << endl;
char s[30];
cin >> s;
int p = 1, m = 1;
while (s) {
int len = strlen(s);
for (int i = 0; i <= (len + 1)/2; i++) {
if (s[i] != s[len - 1 - i]) p = 0;
if (r(s[i]) != s[len - 1 - i]) m = 0;
}
cout << s << msg[m * 2 + p] << endl;
break;
}
return 0;
}
结果:
over
|