Program for Palindrome in C++

0
2482
Finding Length of a string in C++
Creating a Timer 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. 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.

Finding Length of a string in C++
Creating a Timer in C++

Leave a Reply