Program To Check String Is Palindrome Or Not

Prashant | Wed, 03 Jun, 2020 | 354

Program to check string is palindrome or not.

Palindrome string is nothing but string which sound similar when read backward or forward.

For example :- "madam" here we can see that madam is a word sounds exactly same when read from backward or forward.

So how do we chack if a word is palindrome or not.

 

 

 

Step To Check palindrome words

Step 1:- Compare first and last character of a string M 👉 M  which are same

Step 2:- Now increment one from first and go to second character similarly at the end come to second last character.

Step 3:- Now check again if both the characters are equal or not ?

Step 4:- If yes then keep REPEATING above step until all characters are checked.

Step 5:- If all characters are matching then string is palindrome.

 

Program to Check a string is Palindrome or Not

#include <bits/stdc++.h>
 
using namespace std;
void isPalindrome(string str){
    int len=str.length();
    int start=0,end=len-1,flag=0;
    while(start<end){
        if(str[start]!=str[end]){
            flag=1; // reminder for not palindrome
            break;
        }
        start++;
        end--;
    }
    if(flag==0)
    cout<<"palindrome"<<endl;
    else
    cout<<"Not palindrome"<<endl;
}
int main(int argc, char** argv)
{
    string s1="madam";
    string s2="test";
    isPalindrome(s1);
    isPalindrome(s2);
}

 

0 comments
Leave a comment