Just another WordPress.com site

Solved By: সুলতান আহমেদ সাগর
Theory Difficulty: trivial
Coding Difficulty: easy
Algorithms Used: strings
Solution Description: Make sure you don’t forget the empty line after each output.

Keep two booleans, one to check if its a palindrome, and the other to check if its a mirrored string.

If you want to check whether a string is a palindrome it can easily be done . For that purpose you can follow the following code:

bool regular(string a)
{
int i,len=a.length();
bool flag=true;;

for(i=0;i<=len/2;i++)
{
if(a.at(i)!=a.at(len-i-1))
{
flag=false;
break;
}

}
return flag;
}

But to check for a string to be a mirrored string you have to replace half of the characters of the string by its mirror . then check whether it is palindrome or not . If palindrome return true .

bool mirror(string a)
{
char ch[35]={'1','S','E','*','Z','*','*','8','*','A','*','*','*','3','*','*','H','I','L','*','J',
'M','*','O','*','*','*','2','T','U','V','W','X','Y','5'};
int i,len=a.length();
if(len%2==0)
len=len/2-1;
else
len=len/2;
bool flag=true;

for(i=0;i<=len;i++)
{
if( isalpha(a.at(i) ) )
{
if(ch[a.at(i)-'A'+9]=='*' )
return false;
else
a.at(i)=ch[a.at(i)-'A'+9];
}
else if(isdigit(a.at(i)))
{
if(ch[a.at(i)-'1']=='*')
return false;
else
a.at(i)=ch[a.at(i)-'1'];
}
}
return regular(a);
}

Make sure that you set palindrome to true if the length = 1, in all cases.

Code:

#include
#include
#include
using namespace std;

bool regular(string a)
{
int i,len=a.length();
bool flag=true;;

for(i=0;i<=len/2;i++)
{
if(a.at(i)!=a.at(len-i-1))
{
flag=false;
break;
}

}
return flag;
}

bool mirror(string a)
{
char ch[35]={'1','S','E','*','Z','*','*','8','*','A','*','*','*','3','*','*','H','I','L','*','J',
'M','*','O','*','*','*','2','T','U','V','W','X','Y','5'};
int i,len=a.length();
if(len%2==0)
len=len/2-1;
else
len=len/2;
bool flag=true;

for(i=0;i>a)
{
reg=regular(a);
mir=mirror(a);

if( reg && mir )
{
cout<<a;
printf(" — is a mirrored palindrome.\n\n");
}
else if( !reg && mir )
{
cout<<a;
printf(" — is a mirrored string.\n\n");
}
else if( reg && !mir )
{
cout<<a;
printf(" — is a regular palindrome.\n\n");
}
else if( !reg && !mir )
{
cout<<a;
printf(" — is not a palindrome.\n\n");
}

}
return 0;
}

Test Case :

Input :

NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
MAIAM
123ESI
123ES1
DEO3D
9339
A
B
E
M
1
3
2
4

Output :

NOTAPALINDROME — is not a palindrome.

ISAPALINILAPASI — is a regular palindrome.

2A3MEAS — is a mirrored string.

ATOYOTA — is a mirrored palindrome.

MAIAM — is a mirrored palindrome.

123ESI — is not a palindrome.

123ES1 — is a mirrored string.

DEO3D — is not a palindrome.

9339 — is a regular palindrome.

A — is a mirrored palindrome.

B — is a regular palindrome.

E — is a regular palindrome.

M — is a mirrored palindrome.

1 — is a mirrored palindrome.

3 — is a regular palindrome.

2 — is a regular palindrome.

4 — is a regular palindrome.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: