uva--401 Palindromes

题目:Palindromes

解题思路:判断是否为回文:判断字符串前n/2的第i个字符与第n-1-i个字符是否相同,都相同就是回文;判断是否为mirrored,判断字符串前n/2字符的第n-1-i个字符是否与第i个的镜像字符相同,都相同则是mirrored;要注意每个输出后面都要再打印一行空白行;

#include<stdio.h>
#include<string.h>

const int N = 50;
char *s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
char *s2 = "A000300HIL0JM0O0002TUVWXY51SE0Z0080";

bool ispalindrome(char * s) {

	int i, n = strlen(s);
	for (i = 0;i <= n/2; i++)
		if (s[i] != s[n-1-i])
			return false;
		return true;
}
bool ismirrored (char * s) {

	int i,j,n = strlen(s);
	for (i = 0; i <= n/2; i++) {
		
		for (j = 0;s1[j] && s[i] != s1[j]; j++);
		if (s1[j])
			if (s[n-1-i] != s2[j])
				return false;
	}
	return true;
}
int main() {

	
	char s[N];
	bool m, p;
	int i,j;
	while (scanf("%s", &s) == 1) {

		p=ispalindrome(s);
		m=ismirrored(s);
		if(m && p )
			printf("%s -- is a mirrored palindrome.\n",s);
		else if (p)
			printf("%s -- is a regular palindrome.\n",s);
		else if (m)
			printf("%s -- is a mirrored string.\n",s);
		else 
			printf("%s -- is not a palindrome.\n",s);
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(uva--401 Palindromes)