Problem Three
Palindromes
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 elementsof the string is changed to its reverse (if it has a reverse) andthe string is read backwards the result is the same as the originalstring. for example, the string"3 aiae"Is a mirroredstring because""And"I"Are their ownreverses, 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. thestring"Atoyota"Is a mirrored palindrome because if thestring is read backwards, the string is the same as the originaland because if each of the characters is replaced by its reverseand the result is read backwards, the result is the same as theoriginal string. of course,"","T","O", And"Y"Are all their own reverses.
A list of all valid characters and their reverses is asfollows.
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 |
|
|
NoteThat O (zero) and 0 (the letter) areconsidered the same character and thereforeOnlyThe letter "0" is a valid character.
Input
Input consists of strings (one per line) each of which willconsist of one to twenty valid characters. There will be no invalidcharacters in any of the strings. Your program shocould read to theend of file.
Output
For each input string, you shocould print the string starting incolumn 1 immediately followed by exactly one of the followingstrings.
String |
Criteria |
"-- Is nota palindrome." |
If the string isnot 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 |
NoteThat the output line is to include-'S and spacing exactly as shown in the table above anddemonstrated in the sample output below.
In addition, after each output line, you must print an emptyline.
Sampleinput
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.
Code
# Include "stdio. H"
# Include
Char const jxhw [] = "A 3 Hil JM o 2tuvwxy51se Z 8 ";
Char substring (char ch)
{
Char;
A = CH-'A ';
If (A> = 0)
Return jxhw [CH-'a'];
Return jxhw [CH-'0' + 25];
}
Int main ()
{
Char indium [99];
While (scanf ("% s", indium )! = EOF)
{
Int Len = strlen (indium );
Int I, jingxiang = 1, Huiwen = 1;
For (I = 0; I <(LEN + 1)/2; I ++)
{
If (indium [I]! = Indium [len-i-1])
Huiwen = 0;
If (then (indium [I])! = Indium [len-i-1])
Jingxiang = 0;
}
If (jingxiang = 0 & Huiwen = 0)
Printf ("% s -- is not a palindrome. \ n", indium );
If (jingxiang = 0 & Huiwen = 1)
Printf ("% s -- is a regular palindrome. \ n", indium );
If (jingxiang = 1 & Huiwen = 0)
Printf ("% s -- is a mirrored string. \ n", indium );
If (jingxiang = 1 & Huiwen = 1)
Printf ("% s -- is a mirrored palindrome. \ n", indium );
}
Return 0;
}
My way
I thought about this question for a long time. At the beginning, I had no clue. Later, after thinking and clarifying the clues, I began to finish this question.
First, use const to define an array and list all the images used (in fact, you do not need to use const, so be prepared ). At the beginning of the program, a long enough array is defined to store input. Use while and EOF to loop input. Then, the length of the input string is determined. The length is used to calculate the number of times that need to be calculated in the for loop. Before the loop, both jingxiang and Huiwen variables are defined as one (yes ). In a loop, the two if statements are used to determine whether the image is a return object or an image. Finally, by judging whether jingxiang and Huiwen are true or false, you can determine whether they are images, replies, or both.
The question is not difficult, but due to the lack of skilled application of knowledge, it takes a long time for me to do it. Look at those seconds of AC, you still have a long way to go.
Problem 003 -- palindromes