Just another WordPress.com site

Simple problem. You need to find whether the given string is a palindrome or not. But you must consider some extra thing while need to ignore some other things.

To Consider : You need to consider the string as a case non-sensitive one.
To Ignore :  ‘.’, ‘,’, ‘!’, ‘?’ and blank spaces.

Example

Madam, Im adam!
After removing the ignoring items we get,
MadamImadam
As they are all case non-sensitive, it looks,
madamimadam
which is a palindrome.

Alternative: Ad-hoc. Read in each line of input check whether the alphabets are same with two iterators one from the beginning and one from the end. Skip non-alphabetical characters and lowercase the characters before comparing. Output “You won’t be eaten!” if it is a palindromic sentence else output “Uh oh..”.

Code :

#include<iostream>
#include<string>
using namespace std;

int main()
{
string a,b="";
int i,flag=1,len;
while(1)
{
getline(cin,a);
if(a.compare("DONE")==0)
break;
for(i=0;i<a.length();i++)
if(isalpha(a.at(i)))
b+=toupper(a.at(i));
len=b.length();
for(i=0;i<(len+1)/2;i++)
if(b.at(i)!=b.at(len-i-1))
{
flag=0;
break;
}
if(flag==0)
cout<<"Uh oh.."<<endl;
else
cout<<"You won't be eaten!"<<endl;
b="";
flag=1;
}
return 0;
}

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

%d bloggers like this: