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
Post A Comment:
0 comments: