C++ Program to Check String is Palindrome or not

Here you will get C++ program to check string is palindrome or not.

A string is called palindrome if it is equal to its reverse. For example bob is string palindrome.

#include<iostream>

using namespace std;

int main()
{
	int i,j,len,flag=1;
	char a[20];
	
	cout<<"Enter a string:";
	cin>>a;
	
	for(len=0;a[len]!='\0';++len);
	
	for(i=0,j=len-1;i<len/2;++i,--j)
	{
		if(a[j]!=a[i])
			flag=0;
	}
	
	if(flag==1)
		cout<<"\nThe string is Palindrome";
	else
		cout<<"\nThe string is not Palindrome";

	return 0;
}

Output

Enter a string:saas

The string is Palindrome

19 thoughts on “C++ Program to Check String is Palindrome or not”

    1. A string is palindrome if first half is equal to second half. Therefor the 2nd loop run till half of the length of string.

    1. This if for getting the length of appropriate array. after writing a word or a paragraph the length is pointing at a final index. which is the length of an array.

  1. How can I write a c++ program that print the ff…
    5
    4 5 4
    3 4 5 4 3
    2 3 4 5 4 3 2
    1 2 3 4 5 4 3 2 1
    please help me…
    Thanks!

    1. #include

      int main()
      {
      int i,j,k;
      for(i = 5; i >= 1; i–)
      {

      for(j = i; j<5; j++)
      {
      std::cout <=i; k–)
      {
      std::cout << k;

      }
      std::cout<<std::endl;
      }
      return 0;
      }

      1. But, It is somehow tricky if the output is

        5
        4 5 4
        3 4 5 4 3
        2 3 4 5 4 3 2
        1 2 3 4 5 4 3 2 1

        I wonder if you do it in c++.
        Thanks anyways.

  2. sir how can i make the output like this
    *
    **
    **
    ***
    ***
    ***
    ****
    ****
    ****
    ****
    *****
    *****
    *****
    *****
    ***** and
    1
    23
    456
    78910

  3. my compiler is giving problem in first for loop for(len=0;a[len]!=’’;++len); can you plz tell me what should i do to remove that problem.

Leave a Comment

Your email address will not be published. Required fields are marked *