例题3-3 回文词(Palindromes,UVa401)

原题链接:https://vjudge.net/problem/UVA-401
分类:字符串
备注:水题、映射
思路:判断回文字符就是前后对称比较很容易,判断镜像如果一个个写出来就很麻烦,造常量数组,再用一定的方法使得每个字符能映射到对应的镜像字符进而判断是否为镜像字符串。
代码如下:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
const char* out[] = { "is not a palindrome.","is a regular palindrome.","is a mirrored string.","is a mirrored palindrome." };
const char s2[] = { "A   3  HIL JM O   2TUVWXY51SE Z  8 " };
bool check1(char *s)
{
	int len = strlen(s);
	for (int i = 0; i < len - i - 1; i++ ) 
		if (s[i] != s[len - i - 1])return false; 
	return true;
}
bool check2(char *s)
{
	int len = strlen(s), temp;
	for (int i = 0; i <= len - i - 1; i++)
	{
		if (isalpha(s[i]))temp = s[i] - 'A';
		else temp = s[i] - '0' + 25;
		if (s2[temp] != s[len - i - 1])return false;
	}
	return true;
}
int main(void)
{
	char s[1000];
	while (~scanf("%s",s))
	{
		int tt = 0;
		if (check1(s))tt += 1;
		if (check2(s))tt += 2;
		printf("%s -- %s\n\n", s, out[tt]);
	}
	return 0;
}
发布了22 篇原创文章 · 获赞 23 · 访问量 518

猜你喜欢

转载自blog.csdn.net/TK_wang_/article/details/104350157