Palindrome Program in C++

In this tutorial (Palindrome Program in C++) we going to talk about a C++ program to check the string if it is palindrome or not. The program used for executing this code is Microsoft Visual Studio which has many version last one is 2011.Visual studio can be used to create Windows applications (or Desktop applications) and also we can create console applications, which use a black window (Command Line Interface cmd) for viewing and getting data to and from user.The code next will be a console application, where user will have to enter the string to be checked.

  • The idea of the program is simply to check the similarity of the first and last characters, then the second and the before last character and so on.
  • If the program found that all comparisons are equal, it will output: Palindrome, otherwise it will output:Not Palindrome.

Palindrome Program in C++ Code

#include#includeint main(){char strn[80];cout<<"Enter the string: ";cin.getline(strn,80);int len=strlen(strn);        bool flag=true;  // create a Boolean value, "flag" to be used in our loopfor(int c=0;c!=len/2;c++) // do a loop from 0 to half the length of the string{if(flag) // if it is a palindrome so far{if(strn[c]!=strn[len-c-1]) // check the characters match{flag=false; // if they don't set the indicator to false}}else{break; // if it is not a palindrome, exit the for loop}}// if flag is true cout "Palindrome" otherwise output "Not Palindrome"if(flag){cout<<"Palindrome";}else{cout<<"Not Palindrome";}cin.get();return 0;}

Palindrome Program in C++ Explanation


These includes some of the built-in functions in C++, where:- iostream is to include the functions of input and output which are in this program: cin and cout.- string is to enable us to use strings in the program as string is not a data type in C++ and it should be included at the start of the program.

int main()

This is our main function, where any C++ program should have one and only one main function which is the heart of the program.You can write the whole program in the main as we did here, or you can write many functions out of the main, but you will HAVE TO call them from main.

{char strn[80];

This is a declaration sentence, where in this case we declare a variable of type character, but in this case it is an array of 80 characters as maximum length named strn.

cout<<"Enter the string: ";

This line outputs the sentence Enter the string: on the console interface and this is what cout keyword is used to.


This line is to input the string from the user through the console interface, and this is what cin.getline is used to.

int len=strlen(strn);

This line declares a variable of type integer namedlen.The value of len is the length of the strn which we entered previously.

bool flag=true; 

Creates a variables of type Boolean, which takes only one of two values, either true or false.

for(int c=0;c!=len/2;c++){

This initiates for loop which is a way to execute a specific part of the code more than one time, and it works as follows: First we use a variable which is c in this example. armbandjes kinderen . training solutions . We define a starting and ending value of c and a value for it to use it as increment.In this case we will initiate c with the value zero, and as c didnt reach the final value which will be the half of the length of the string, it continues the loop with increment of one ++.


If statement is used to check a condition: if the result is true, it executes the part of code in the next braces, which will be done here as the value of flag is true . design bbq .


This statement is to check the characters match, as it checks at first if the value of the first character is equal or not to the value of the last character;If they are equal it wont execute the code in the next braces as here I used the condition of not-equal indicated by != .This operation will continue for the next characters; second and before last and so on.


Again, this part will be executed if the characters are not similar, and it tends to change the value of flag to false not to enter the operation of the parent if-statement.


else-statement is executed if if-statement is not executed


if it is not a palindrome, exit the for loop if flag is true cout “Palindrome” otherwise output “Not Palindrome”

if(flag){cout<<"Palindrome";}else{cout<<"Not Palindrome";}cin.get();return 0;}
  • umar sajjad

    It’s preety cool, this program really helps me a lot
    Please keep on posting more articles relating programming languages.

  • shashikant

    why we take flag in this program