C++ Program to check whether given string is palindrome or not [DEVCPP/GCC]


PALINDROME

A palindrome is a word or phrase which is read same from forward as well as from backwards. Spaces or punctuations are usually not taken into consideration for Palindromes.

WORD PALINDROMES
  • Madam
  • Racecar
  • Level
  • Radar
  • Refer
PHRASE PALINDROMES
  • Draw pupil's lip upwards.
  • Rise to vote, Sir!
  • Step on no pets!

PROGRAM

// Program to check whether given string is palindrome or not

#include <iostream>
#include <string>

using namespace std;

int main()
{
int i,len;
string str,strws;
bool flag=true;
cout<<"ENTER A STRING: ";
getline(cin,strws);
len=strws.length();
/*Removing Spaces and Punctuations From String and Converting into Lowercase*/
for(i=0;i<len;i++)
{
if('A'<=strws[i] && strws[i]<='Z')
{
strws[i]=strws[i]+32;
}
if('a'<=strws[i] && strws[i]<='z')
{
str.push_back(strws[i]);
}
}

len=str.length();

for(i=0;i<=(len/2);i++)
{
if(str[i]!=str[len-i-1])
{
flag=false;
break;
}
}
if(flag)
{
cout<<"\nGIVEN STRING IS A PALINDROME!";
}
else
{
cout<<"\nGIVEN STRING IS NOT A PALINDROME!";
}
return 0;
}

OUTPUT

C++ Program to check whether given string is palindrome or not with output

Share this

Related Posts

FIND US ON FACEBOOK!