Home coding Write a program to check string is palindrome or not.

Write a program to check string is palindrome or not.

0
108

Given a string we need to check whether string is palindrome or not.and print their result on screen.

So what is palindrome ?

if left side of character is same as right side of character then it is palindrome.

Example:

input:abcba
output:true

Logic:

we need to check if the first character is same as the last character,then  it is palindrome 

Code-

#include<bits/stdc++.h>
using namespace std;
int main()
{
   string s="abcba";
    int n=s.length();
    int flag=0;
    for(int i=0;i<n/2;i++)
    {
        if(s[i]!=s[n-i-1])
        {
            flag=1;
        }
       
    }
 if(flag==0)
 {
     cout<<"palindrome"<<endl;
 }
 else
 {
     cout<<"not palindrome"<<endl;
 }
  
   
    
    
}

Output:

palindrome

Time complexity:0(N)

Space Complexity:O(1)

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here