Program to Check if a String is Palindrome or Not
2025-11-04
python
c
c#
js
php
palindrome
beginner
interview-question
This program checks whether a given string reads the same forward and backward. A string like madam, racecar, 121, or malayalam is considered a palindrome because its characters remain the same even after reversing.
Program Explanation
What is a Palindrome?
A palindrome is a string that remains unchanged when reversed.
Steps to Solve
- Take a string as input.
- Reverse the string using built-in functions or manual logic.
- Compare the original string with the reversed string.
- If both are equal → it is a palindrome.
- Otherwise → it is not a palindrome.
Key Points
- Palindrome checking is usually case-sensitive unless handled separately.
- Spaces and special characters can be ignored based on requirement.
- This program checks the raw input directly.
Program Code
s = input("Enter a string: ")
rev = s[::-1]
if s == rev:
print("Palindrome")
else:
print("Not a palindrome")
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
printf("Enter a string: ");
scanf("%s", str);
strcpy(rev, str);
strrev(rev);
if (strcmp(str, rev) == 0)
printf("Palindrome");
else
printf("Not a palindrome");
return 0;
}
let s = prompt("Enter a string:");
let rev = s.split("").reverse().join("");
if (s === rev)
console.log("Palindrome");
else
console.log("Not a palindrome");
using System;
class Program {
static void Main() {
Console.Write("Enter a string: ");
string s = Console.ReadLine();
char[] arr = s.ToCharArray();
Array.Reverse(arr);
string rev = new string(arr);
if (s == rev)
Console.WriteLine("Palindrome");
else
Console.WriteLine("Not a palindrome");
}
}
<?php
$s = readline("Enter a string: ");
$rev = strrev($s);
if ($s == $rev) {
echo "Palindrome";
} else {
echo "Not a palindrome";
}
?>
