A regular palindrome is a string of numbers or letters that is thesame forward as backward. For example, the string "ABCDEDCBA" is apalindrome because it is the same when the string is read from left to right aswhen the string is read from right to left.
A mirrored string is a string for which when each of the elements of the stringis changed to its reverse (if it has a reverse) and the string is readbackwards the result is the same as the original string. For example, thestring "3AIAE" is a mirroredstring because "A" and "I" aretheir own reverses, and "3" and "E" areeach others' reverses.
A mirrored palindrome is a string that meets the criteria of a regularpalindrome and the criteria of a mirrored string. The string "ATOYOTA" is a mirroredpalindrome because if the string is read backwards, the string is the same asthe original and because if each of the characters is replaced by its reverseand the result is read backwards, the result is the same as the originalstring. Of course, "A", "T", "O", and "Y" areall their own reverses.
A list of allvalid 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) and0 (the letter) are considered the same character and therefore ONLY the letter"0" is a valid character.
Inputconsists of strings (one per line) each of which will consist of one to twentyvalid characters. There will be no invalid characters in any of the strings.Your program should read to the end of file.
Foreach input string, you should print the string starting in column 1 immediatelyfollowed 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 theSample Output below.
In addition, aftereach output line, you must print an empty line.
ISAPALINILAPASI
2A3MEAS
NOTAPALINDROME -- is not a palindrome.
ISAPALINILAPASI -- is a regular palindrome.
2A3MEAS -- is a mirrored string.
ATOYOTA -- is a mirrored palindrome.
代码:
#include<cctype>
#include<iostream>
#include<string>
usingnamespace std;
chartr(char old);
intmain()
{
string test;
string print[]={" -- is not apalindrome.",
" -- is a regular palindrome.",
" -- is a mirrored string.",
" -- is a mirrored palindrome."};
while(cin>>test)
{
int p=1,q=1;
int n=test.size();
for(int i=0; i<=(n-1)/2; i++)
{
if(test[i]!=test[n-1-i])
{
p=0;
}
if(tr(test[i])!=test[n-1-i])
{
q=0;
}
}
cout<<test<<print[p+2*q]<<endl<<endl;
}
}
chartr(char old)
{
string root="A 3 HILJM O 2TUVWXY51SE Z 8 ";
if(isalpha(old))
{
return root[old-'A'];
}
else
{
return root[old-'0'+25];
}
}
注释:
isalpha()方法:#include<cctype> if(isalpha(char ch))注意:该方法返回值类型为int,是字母返回非0的值,不是字母返回0.非0为TRUE,0为FALSE。