UVa -- 401 Palindromes
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.
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 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.
STRING | CRITERIA |
" -- 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 中定义的函数:
序号 | 函数 & 描述 |
---|---|
1 | int isalnum(int c) 该函数检查所传的字符是否是字母和数字。 |
2 | int isalpha(int c) 该函数检查所传的字符是否是字母。 |
3 | int iscntrl(int c) 该函数检查所传的字符是否是控制字符。 |
4 | int isdigit(int c) 该函数检查所传的字符是否是十进制数字。 |
5 | int isgraph(int c) 该函数检查所传的字符是否有图形表示法。 |
6 | int islower(int c) 该函数检查所传的字符是否是小写字母。 |
7 | int isprint(int c) 该函数检查所传的字符是否是可打印的。 |
8 | int ispunct(int c) 该函数检查所传的字符是否是标点符号字符。 |
9 | int isspace(int c) 该函数检查所传的字符是否是空白字符。 |
10 | int isupper(int c) 该函数检查所传的字符是否是大写字母。 |
11 | int isxdigit(int c) 该函数检查所传的字符是否是十六进制数字。 |
标准库还包含了两个转换函数,它们接受并返回一个 "int"
序号 | 函数 & 描述 |
---|---|
1 | int tolower(int c) 该函数把大写字母转换为小写字母。 |
2 | int 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 | 字母字符 小写字母和大写字母的集合。 |