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

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

输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。所谓回文串,就是反转以后和原串相同,如abba和madam。所有镜像串,就是左右镜像之后和原串相同,如2S和3AIAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。在本题中,每个字符的镜像如图3-3所示(空白项表示该字符镜像后不能得到一个合法字符)。


输入的每行包含一个字符串(保证只有上述字符。不含空白字符),判断它是否为回文串和镜像串(共4种组合)。每组数据之后输出一个空行。

样例输入:

NOTAPALINDROME

ISAPALINILAPASI

2A3MEAS

ATOYOTA

样例输出:

NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS -- is a mirrored string.

ATOYOTA -- is a mirrored palindrome.

【分析】

既然不包含空白字符,可以安全地使用scanf进行输入。回文串和镜像串的判断都不复杂,并且可以一起完成,详见下面的代码。使用常量数组,只用少量代码即可解决这个看上去有些复杂的题目。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
//常量字符串,存在镜像列表,与大写字母列表的顺序是对应的
const char* rev = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";//一定要与列表对应,特别是空格
const char* msg[] = {"not a palindrome", "a regular palindrome", 
					"a mirrored string", "a mirrored palindrome"};
char r(char ch) {//返回ch的镜像字符
	if(isalpha(ch)) return rev[ch - 'A'];
	else return rev[ch - '0' + 25];//如果是数字,返回镜像列表中对应的镜像
}
int main() {
	char s[30];
	while(scanf("%s", s) == 1) {
		int len = strlen(s);
		int p = 1, m = 1;
		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; //不是镜像串
		}
		printf("%s -- is %s.\n\n", s, msg[m*2+p]);
	}
	return 0;
}

原文如下:

A regular palindrome is a string of numbers or letters that is thesame forward as backward. For example, the string “ABCDEDCBA” is a palindromebecause it is the same when the string is read from left to right as when thestring is read from right to left.

A mirrored string is a string for which when each of the elements ofthe string is changed to its reverse (if it has a reverse) and the string isread backwards the result is the same as the original string.

For example, the string “3AIAE” is a mirrored string because ‘A’ and‘I’ are their own reverses, and ‘3’ and ‘E’ are each others’ reverses.

A mirrored palindrome is a string that meets the criteria of aregular palindrome and the criteria of a mirrored string. The string “ATOYOTA”is a mirrored palindrome because if the string is read backwards, the string isthe same as the original and because if each of the characters is replaced by itsreverse and the result is read backwards, the result is the same as theoriginal string. Of course, ‘A’, ‘T’, ‘O’, and ‘Y’ are all their own reverses.

A list of all valid characters and their reverses is as follows.

Character     Reverse     Character           Reverse     Character   Reverse

         A               A               M                      M                Y               Y

         B                                 N                                          Z               5

         C                                 O                       O                1               1

         D                                 P                                          2               S

         E               3                Q                                          3              E

         F                                  R                                         4

         G                                 S                         2               5               Z

         H              H                T                         T               6

         I                I                 U                        U               7

         J                L                V                        V                8               8

         K                                 W                       W              9

         L               J                 X                         X

Note that ‘0’ (zero) and ‘O’ (the letter) are considered the samecharacter and therefore ONLY the

letter ‘O’ is a valid character.

Input

Input consists of strings (one per line) each of which will consistof one to twenty valid characters.

There will be no invalid characters in any of the strings. Yourprogram should read to the end of file.

Output

For each input string, you should print the string starting incolumn 1 immediately followed by exactly

one of the following strings.

                            STRING                                                                                      CRITERIA

‘ -- is not apalindrome.’               if the stringis not a palindrome and is not a mirrored string

‘ -- is aregular palindrome.’    if the string isa palindrome and is not a mirrored string

‘ -- is amirrored string.’       if the string isnot a palindrome and is a mirrored string

‘ -- is amirrored palindrome.’   if the string isa palindrome and is a mirrored string

Note that the output line is to include the ‘-’s and spacing exactlyas shown in the table above and

demonstrated in the Sample Output below.

In addition, after each output line, you must print an empty line.

Sample Input

NOTAPALINDROME

ISAPALINILAPASI

2A3MEAS

ATOYOTA

Sample Output

NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS -- is a mirrored string.

ATOYOTA -- is a mirrored palindrome.

提示3-20:头文件ctype.h中定义的isalpha、isdigit、isprint等工具可以用来判断字符的属性,而toupper、tolower等工具可以用来转换大小写。如果ch是大写字母,则ch-'A'就是它在字母表中的序号(A的序号是0,B的序号是1,依此类推);类似地,如果ch是数字,则ch-'0'就是这个数字的数值本身。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值