Palindrome program in C++ | To check if given string is palindrome

Palindrome in C++

A palindrome is a word, number, phrase, or other groupings of characters which peruses indistinguishable backward from forward, for example, madam or racecar or the number 10801.


Palindrome Algorithm

  • get the input data from the user
  • allocate the input data in temporary variable
  • reverse the input data
  • compare the temporary input data with reversed input data
  • if both input data are same, then it is palindrome
  • otherwise not a palindrome



Palindrome Program

#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cout<<"Enter a word : ";
cin>>a;
int i,j,len=a.length();
bool flag=true;
for(i=0,j=len-1;i<len/2;i++,j--)
{
if(a.at(i)!=a.at(j))
{
flag=false;
break;
}
}
if(flag) cout<<"Word is Palindrome";
else cout<<"Word is not Palindrome";
return 0;
}
OUTPUT
Enter a word : malayalam
Word is Palindrome



tags: 
palindrome in C++, palindrome in C, palindrome algorithm, program to check if a given string is a palindrome, programs of C. 

Post a Comment

Previous Post Next Post