Thursday, March 4, 2021
Home C / C++ Program for Palindrome in C++

Program for Palindrome in C++

Palindrome is a word that reads the same forward or backward. Now how can we check a string that it is a palindrome or not? We can do it by using simple character type of arrays. Here we have a Program for Palindrome in C++ :

Program for Palindrome in C++

Lets have a look at the given code:

 //Program for palindrome in c++
//checking a string for Palindrome.
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
int main()
{
char strn[80];

int i,j,flag=0,len;
cout<<"Enter the string:";
cin.getline(strn,80);

len=strlen(strn);

for(i=0,j=len-1;i<=(len/2);++i,--j)
{

if(strn[i]==strn[j])
flag=1;

}

if(flag)
cout<<" the given string is a Palindrome";

else cout<<"The given string is NOT a palindrome";

}

Now in the above code for checking the palindrome, we used a simple array of type char and find out the length of the string through a function named “strlen()”. Then we checked the array from both forward and backward sides. For this checking we used a “for” loop, and then used an “if” condition which then decides that the given string is palindrome or not a palindrome.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....