This is a simple string processing problem. You need to determine whether the given string is a text return or a "mirror string ".
Retrieval judgment: you only need to judge whether the first and last characters of the string are the same, and then move the first and last characters to the middle to judge.
"Mirror string" judgment: First replace the original string with the reverse string, and then compare it with the original string to see if it is the same.
My code is as follows:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>using namespace std;char input[25];const char CharReverse[] = "A 3 HIL JM O 2TUVWXY5";const char NumReverse[] = "01SE Z 8 ";int is_palindrome(){int length=strlen(input);int i=0, j=length-1;while(i<=j){if(input[i++]!=input[j--]) break;}if(i>j) return 1;else return 0;}int is_mirrored(){char Reverse[25];int length=strlen(input);for(int i=0; i<length; i++){if(input[i]>='A' && input[i]<='Z')Reverse[length-i-1]=CharReverse[input[i]-'A'];else if(input[i]>='0' && input[i]<='9')Reverse[length-i-1]=NumReverse[input[i]-'0'];}Reverse[length]='\0';if(strcmp(input,Reverse)==0) return 1;else return 0;}int main(){int flag_palindrome,flag_mirrored;while(cin >> input){flag_palindrome = flag_mirrored = 0;flag_palindrome = is_palindrome();flag_mirrored = is_mirrored();cout << input;if(!flag_palindrome && !flag_mirrored)cout << " -- is not a palindrome.\n\n";else if(flag_palindrome && !flag_mirrored)cout << " -- is a regular palindrome.\n\n";else if(!flag_palindrome && flag_mirrored)cout << " -- is a mirrored string.\n\n";else if(flag_palindrome && flag_mirrored)cout << " -- is a mirrored palindrome.\n\n";}return 0;}