Va 401: palindromes

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.