Printing string palindrome in C++


This is a C++ program that checks whether a given string is a palindrome or not. A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward. For example, "racecar" is a palindrome. The program works as follows:

  • The user is prompted to enter a string, which is read in using the cin object and stored in the string variable a.
  • The length of the string a is calculated by iterating over the characters in the string using a for loop and counting the number of characters.
  • A new string b is created to store the reverse of the original string a. This is done by iterating over the characters in a using a for loop, and copying each character to b in reverse order.
  • The program then checks whether the original string a and the reversed string b are equal by comparing the characters at each position in the two strings using another for loop. If a character in a is equal to the corresponding character in b, a counter variable s is incremented.
  • If s is equal to the length of the string a, then the original string is a palindrome and the program prints a message to that effect. Otherwise, the program prints a message indicating that the original string is not a palindrome.

One potential issue with this program is that the reversed string b is not initialized before it is used to store the reversed characters of the original string a. This can lead to undefined behavior, as the program is accessing memory that has not been allocated for the string b. To fix this, the string b should be initialized to the same length as a before the loop that reverses the string is executed.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,n=0,s=0;
    string a,b;
    cout<<"\nEnter the string:";
    cin>>a;
    for(i=0;a[i]!='\0';i++)
    {
        n++;
    }
    for(i=0;i<n;i++)
    {
        b[i]=a[n-i-1];
    }
    for(i=0;i<n;i++){
        if(b[i]==a[i]){
            s++;
        }
    }
    if(s==n)
    {
        cout<<"\nThis is a palindrome:"<<a;
    }
    else
    {
        cout<<"\nThis is not a palindrome:"<<a;
    }
    return 0;
}
To download raw file Click Here

Output

Enter the string:madam
This is a palindrome:madam

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples