Write a C++ program to check a string is a palindrome or not.

Program Code:

#include<iostream>
using namespace std;
int main( )
{
int i, j, length=0; //Variable to hold length of string 
char name[30];
cout<<"Enter your string: ";
cin.getline(name, 30);
cout<<endl;
for(i = 0; name[i] != '\0'; i++)   // loop for finding length of string
{
length = length + 1;
}
// Now comparing first element with last element till middle of string content 
for(j = 0; (j < length/2) && (name[j] == name[length - j - 1]); j++);
if(j == length/2)
{
cout << " String is a Palindrome";
}
else
{      
cout << "String is not a palindrome";
}
return 0;
}

This program is tested on www.jdoodle.com

Output1:
Enter your string: abcdcba
String is a Palindrome

Output2:
Enter your string: abcdcbaec
String is not a Palindrome

Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: