UVa -- 401 Palindromes

本文解析了UVa-401 Palindromes问题,介绍了回文串与镜像串的概念,展示了如何通过C++代码判断字符串是否为回文串或镜像串,并附上了样例输入输出。此外,还概述了C标准库ctype.h头文件中的字符测试和映射函数。

UVa-401 Palindromes

Description

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

A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read 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 a regular palindrome and the criteria of a mirrored string. The string "ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original 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.

CharacterReverseCharacterReverseCharacterReverse
AAMMYY
B N Z5
C OO11
D P 2S
E3Q 3E
F R 4 
G S25Z
HHTT6 
IIUU7 
JLVV88
K WW9 
LJXX  

Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter "0" is a valid character.

Input

Input consists of strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings. Your program should read to the end of file.

Output

For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.

STRINGCRITERIA
" -- is not a palindrome."if the string is not a palindrome and is not a mirrored string
" -- is a regular palindrome."if the string is a palindrome and is not a mirrored string
" -- is a mirrored string."if the string is not a palindrome and is a mirrored string
" -- is a mirrored palindrome."if the string is a palindrome and is a mirrored string

Note that the output line is to include the -'s and spacing exactly as 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.

  常量数组的使用

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 using namespace std;
 5 char* rev = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
 6 const char* print[] = {" -- is not a palindrome."," -- is a regular palindrome.",
 7                 " -- is a mirrored string."," -- is a mirrored palindrome."};
 8 char r(char ch)
 9 {
10     if(isalpha(ch)) return rev[ch - 'A'];
11     return rev[ch - '0' + 25];
12 }
13 int main()
14 {
15     char s[30];
16     while(scanf("%s",s) == 1)
17     {
18         int len = strlen(s);
19         int re = 1,p=1;
20         for(int i=0;i<(len+1)/2;i++)
21         {
22             int j = len-i-1;
23             if(s[i] != s[j]) p = 0;//不是回文词
24             if(r(s[i]) != s[j]) re = 0;//不是镜像文
25         }
26         if(p==0 && re==0)
27         {
28             cout<<s<<print[0]<<endl<<endl;
29         }else if(p==1 && re ==0)
30         {
31             cout<<s<<print[1]<<endl<<endl;
32         }else if(p==0 && re==1)
33         {
34             cout<<s<<print[2]<<endl<<endl;
35         }else{
36             cout<<s<<print[3]<<endl<<endl;
37         }
38     }
39     return 0;
40 }

 

C 标准库的 ctype.h 头文件提供了一些函数,可用于测试和映射字符。

这些函数接受 int 作为参数,它的值必须是 EOF 或表示为一个无符号字符。

如果参数 c 满足描述的条件,则这些函数返回非零(true)。如果参数 c 不满足描述的条件,则这些函数返回零。

库函数

下面列出了头文件 ctype.h 中定义的函数:

序号函数 & 描述
1int isalnum(int c)
该函数检查所传的字符是否是字母和数字。
2int isalpha(int c)
该函数检查所传的字符是否是字母。
3int iscntrl(int c)
该函数检查所传的字符是否是控制字符。
4int isdigit(int c)
该函数检查所传的字符是否是十进制数字。
5int isgraph(int c)
该函数检查所传的字符是否有图形表示法。
6int islower(int c)
该函数检查所传的字符是否是小写字母。
7int isprint(int c)
该函数检查所传的字符是否是可打印的。
8int ispunct(int c)
该函数检查所传的字符是否是标点符号字符。
9int isspace(int c)
该函数检查所传的字符是否是空白字符。
10int isupper(int c)
该函数检查所传的字符是否是大写字母。
11int isxdigit(int c)
该函数检查所传的字符是否是十六进制数字。

标准库还包含了两个转换函数,它们接受并返回一个 "int"

序号函数 & 描述
1int tolower(int c)
该函数把大写字母转换为小写字母。
2int toupper(int c)
该函数把小写字母转换为大写字母。

字符类

序号字符类 & 描述
1数字
完整的数字集合 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
2十六进制数字
集合 { 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f }
3小写字母
集合 { a b c d e f g h i j k l m n o p q r s t u v w x y z }
4大写字母
集合 {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }
5字母
小写字母和大写字母的集合
6字母数字字符
数字、小写字母和大写字母的集合
7标点符号字符
集合 ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
8图形字符
字母数字字符和标点符号字符的集合
9空格字符
制表符、换行符、垂直制表符、换页符、回车符、空格符的集合。
10可打印字符
字母数字字符、标点符号字符和空格字符的集合。
11控制字符
在 ASCII 编码中,这些字符的八进制代码是从 000 到 037,以及 177(DEL)。
12空白字符
包括空格符和制表符。
13字母字符
小写字母和大写字母的集合。

 

转载于:https://www.cnblogs.com/yxh-amysear/p/8406339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值