Palindrome String

 

Check whether the given string is palindrome or not.

Palindrome : If we reverse the string if it looks the same then it is called as palindrome.

Example

abcdcba is palindrome
aba is palindrome
a is palindrome
abba is palindrome
abcddcb is not palindrome
abc is not palindrome
aac is not palindrome

Algorithm

Time complexity : O(n)

Step 1 : Store the size of string in S
Step 2 : Compare first character and last character and increment both until first character pointer reaches mid character. If anywhere while comparing they are not equal Print “not a palindrome”.
Step 3 : If it reaches mid, Print “is a palindrome”

Algorithm Working

C++ Program

#include<bits/stdc++.h>
using namespace std;

int main()
{
	string S;
	cin>>S;
	
	for(int i=0;i<S.size()/2;i++)
		if(S[i] != S[S.size()-i-1])
			{
				cout<<"String is not palindrome\n";
				return 0;
			}
	cout<<"String is a palindrome\n";
}
Try It


Next > < Prev
Scroll to Top