C Program
#include<stdio.h> #include<string.h> int main() { char s[100]; gets(s); int i=0, j=strlen(s)-1, f=1; while(i<j) if(s[i++] != s[j--]) f=0; printf(f ? "Palindrome" : "Not Palindrome"); }
C Output
Input: madam Output: Palindrome
C++ Program
#include<iostream> using namespace std; int main() { string s; getline(cin, s); int i=0, j=s.size()-1, f=1; while(i<j) if(s[i++] != s[j--]) f=0; cout << (f ? "Palindrome" : "Not Palindrome"); }
C++ Output
Input: racecar Output: Palindrome
JAVA Program
import java.util.*; class P { public static void main(String[] a) { String s = new Scanner(System.in).nextLine(); int i = 0, j = s.length() - 1; while(i < j) if(s.charAt(i++) != s.charAt(j--)) { System.out.print("Not Palindrome"); return; } System.out.print("Palindrome"); } }
JAVA Output
Input: level Output: Palindrome
Python Program
s = input() print("Palindrome" if s == s[::-1] else "Not Palindrome")
Python Output
Input: hello Output: Not Palindrome
In-Depth Learning – Entire Concept in Paragraphs
What Is a Palindrome?
A palindrome is a word, a phrase, or a sequence that reads the same way forward and backward. For instance, "radar", "level", and "madam" are palindromes. When used in strings in programming, a palindrome signifies that the first character is equal to the last, the second is equal to the second last, and so forth.
How the Code Works
In C, C++, and Java, we:
Take the string input.
Use two pointers — i from the beginning and j from the end.
Compare characters: if s[i] != s[j], not a palindrome.
Keep going until you hit the middle.
In Python, it's even easier:
s[::-1] returns the reversed string.
If original and reversed strings are equal, it's a palindrome.
This approach is straightforward and efficient, touching every character only once.
Example
Input: "noon"
Compare: n == n ✅
Compare: o == o ✅
Result: "Palindrome"
Input: "hello"
Compare: h != o ❌
Result: "Not Palindrome"
This reasoning is applicable to both even- and odd-length strings.
Real-Life Analogy
Stand in front of a mirror with a word card. If you see the same word staring back at you, then it's a palindrome — much like "WOW", "MOM", or "NUN". It's symmetrical and looks the same from either end, much like reading a clock at 12:21 or a license plate like 1221.
Where and When Is It Used?
Palindromes are applied in:
Interview questions (very common string problem)
Pattern matching and text processing
Bioinformatics (DNA sequences)
Encryption, hashing, and data integrity
Language processing (symmetrical pattern checking)
Understanding how to check for palindromes comes in handy in constructing more complex string manipulation logic and confirms your grasp of loops, conditions, and indexing.
Time and Space Complexity
Time Complexity: O(n) → each character processed once
Space Complexity:
O(1) in loop-based (in-place comparison)
O(n) in Python slicing (makes a reversed copy)
Python slicing is easy to read but takes extra memory for the reversed copy.
Python-Specific Tip
This one-liner:
python
print("Palindrome" if s == s[::-1] else "Not Palindrome")
is not only succinct — it's very Pythonic. It's ideal for beginner-friendly and competitive coding solutions.
SEO-Optimized Natural Paragraph for Ranking
Want to test if a string is a palindrome in C, C++, Java, or Python? This tutorial illustrates the quickest and most effective methods of identifying palindromes by using loop-based comparison and slice operations. A palindrome is a sentence or word that reads exactly the same backward and forward. It is essential to learn how to verify palindromes as part of the skill of string manipulation and logical control in programming. Regardless of whether you're doing interviews, coding puzzles, or school assignments, this example illustrates good logic, a good use of memory, and intelligent string traversal methods in four of the most important languages.
Social Plugin