401—Palindromes

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.

Input 

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.

Output 

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.

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.

代码:

#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。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值