Programming Examples

Cpp program to check whether string is a palindrome or not


Write a program to check whether string is a palindrome or not

Solution

#include <iostream>
using namespace std;
int main()
{
    char name[20];
    int a, b, flag = 0;
    cout << "Enter your name (in uppercase or lowercase): ";
    cin.getline(name, 20);
    int len = 0;
    for (a = 0; name[a] != '\0'; a++)
    {
        len++;
    }
    b = len - 1;
    for (a = 0; a < len; a++)
    {
        if (name[a] != name[b])
        {
            flag = 1;
            break;
        }
        b--;
    }
    if (flag == 1)
    {
        cout << "String is not a palindrome";
    }
    else
    {
        cout << "String is a palindrome" << endl;
    }
    return 0;
}
Output

Enter your name (in uppercase or lowercase): HANNAH

String is a palindrome

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad