UVA401 -Palindromes

题意:判断字符是否是四种情况,1,普通字符串,2,回文字符串,3镜像字符串,4既是回文又是镜像字符串。

思路:先判断是否为回文串,再判断是否为镜像字符串,注意要处理0与O之间的关系。

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

int main(){
	char s[100];
	int a, b;	
	while (scanf("%s", s) != EOF){
		b = 1, a = 1;	
		int len = strlen(s);
		for(int i = 0; i < len / 2; i++)	
			if (s[i] != s[len - i - 1] && (s[i] != '0' && s[len - i - 1] != 'O' && s[i] != 'O' && s[len - i - 1] != '0')){
				a = 0;
				break;
			}	
		for(int i = 0; i < len; i++){		
			if (s[i] != 'E' && s[i] != '3' && s[i] != 'J' && s[i] != 'L' && s[i] != 'O' && s[i] != '0' && s[i] != 'S' && 
				s[i] != '2' && s[i] != 'Z' && s[i] != '5' && s[i] != 'A' && s[i] != 'H' && s[i] != 'I' && s[i] != 'M' && 
				s[i] != 'T' && s[i] != 'V' && s[i] != 'W' && s[i] != 'X' && s[i] != '8' && s[i] != 'Y' && s[i] != 'U' && s[i] != '1'){ 
						b = 0;	
						break;	
			}	
			else{	
				if (s[i] == 'E' || s[i] == '3' || s[i] == 'J' || s[i] == 'L' || s[i] == 'O' || 
					s[i] == 'S' || s[i] == '5'	|| s[i] == 'Z' || s[i] == '2' || s[i] == '0'){	
					switch(s[i]){	
						case 'E': if (s[len - i - 1] != '3')
								 		b = 0;
										break;
						case '3': if (s[len - i - 1] != 'E')	
										b = 0;
									break;	
						case 'J': if (s[len - i - 1] != 'L')
								 		b = 0;
									break;
						case 'L': if (s[len - i - 1] != 'J')	
										b = 0;
									break;	
						case '0': if (s[len - i - 1] != 'O')	
										b = 0;
									break;	
						case 'S': if (s[len - i - 1] != '2')
								 		b = 0;
									break;
						case '2': if (s[len - i - 1] != 'S')	
										b = 0;
									break;	
						case 'Z': if (s[len - i - 1] != '5')
								 		b = 0;
									break;
						case '5': if (s[len - i - 1] != 'Z')	
										b = 0;
									break;
						}
					}	
				else
					if (s[i] != s[len - i - 1]){
						b = 0;
						break;
					}
			}		
					
		}	
		if (a){
			if (b)	
				printf("%s -- is a mirrored palindrome.\n\n", s);	
			else
				printf("%s -- is a regular palindrome.\n\n", s);
		}
		else{
			if (b)	
				printf("%s -- is a mirrored string.\n\n", s);	
			else 
				printf("%s -- is not a palindrome.\n\n", s);
		}	
	}

	return 0;
}


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