String Palindrome Program in C
Check whether a string reads the same forwards and backwards in C using two indexes.
- Time
O(n), with at most n / 2 comparisons - Space
O(1); no reversed copy is made - Hints3
- Read3 min
To check a string palindrome in C, set left = 0 and right = length - 1. While left < right, compare str[left] with str[right]. If they differ, it is not a palindrome. Otherwise move left forward and right back. If the loop finishes, the string is a palindrome. This needs no reversed copy, takes O(n) time and O(1) extra space. strrev() is not part of standard C, so avoid relying on it.
Problem statement
Write a string palindrome program in C. A string is a palindrome if it reads the
same from left to right and from right to left, for example madam or level.
Use two indexes and compare characters. Do not use strrev() or make a reversed copy.
A string str with no more than 99 characters.
"<str> is a palindrome" or "<str> is not a palindrome".
Examples
str = "madam"madam is a palindromem = m, a = a, and the middle d needs no partner.
str = "hello"hello is not a palindromeThe first comparison is h with o, which differ, so the check stops at once.
Constraints
- 0 <= length <= 99
- The basic check is case-sensitive; a case-insensitive version is shown below
Think before you code
Reveal the questions to ask yourself first
- Which character should the first character be compared with?
- When can you stop comparing?
- What is the middle character of an odd-length string compared with?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Two pointer palindrome in C
Put one index at each end of the string and walk them towards the middle.
| Step | Code |
|---|---|
| Start | left = 0, right = strlen(str) - 1 |
| Compare | if (str[left] != str[right]) return 0; |
| Move | left++, right-- |
| Stop | when left >= right |
For a string of length n, at most n / 2 comparisons are made. The middle
character of an odd-length string, such as d in madam, is never compared,
because it matches itself.
Why not strrev()?
Many textbooks reverse the string with strrev() and compare it with strcmp().
strrev() is not in the C standard. Some old Windows compilers have it, but GCC on
Linux does not, so the program will not compile there. It also needs a second copy
of the string. The two-index method works on every compiler and uses no extra array.
Dry run
Walk through the example step by step
str = "level", length 5:
left | right | str[left] | str[right] | equal?
-----+-------+-----------+------------+-------
0 | 4 | l | l | yes
1 | 3 | e | e | yes
2 | 2 | - | - | stop, left is not < right
No mismatch was found, so level is a palindrome. For "hello", the first row
compares h with o, finds a mismatch, and returns 0.
Solution
Reveal the full C solution
#include <stdio.h>
#include <string.h>
int is_palindrome(const char str[]) {
int left = 0;
int right = strlen(str) - 1;
while (left < right) {
if (str[left] != str[right]) {
return 0;
}
left++;
right--;
}
return 1;
}
int main(void) {
const char *words[] = {"madam", "level", "hello", "a", "abba"};
for (int i = 0; i < 5; i++) {
if (is_palindrome(words[i])) {
printf("%s is a palindrome\n", words[i]);
} else {
printf("%s is not a palindrome\n", words[i]);
}
}
return 0;
}Output:
madam is a palindrome
level is a palindrome
hello is not a palindrome
a is a palindrome
abba is a palindrome
"a" has one character, so left = 0 and right = 0, the loop never runs, and the
function returns 1. "abba" has even length: pairs (a, a) and (b, b) match, then
left becomes 2 and right becomes 1, and the loop stops.
Check palindrome string in C with user input, no library needed
This version reads a line with fgets, finds the length with its own loop, and does
not include string.h at all.
#include <stdio.h>
int main(void) {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int len = 0;
while (str[len] != '\0' && str[len] != '\n') {
len++;
}
str[len] = '\0';
int i = 0, j = len - 1, palindrome = 1;
while (i < j) {
if (str[i] != str[j]) {
palindrome = 0;
break;
}
i++;
j--;
}
if (palindrome) {
printf("\"%s\" is a palindrome\n", str);
} else {
printf("\"%s\" is not a palindrome\n", str);
}
return 0;
}
Enter a string: racecar
"racecar" is a palindrome
Enter a string: Nitin
"Nitin" is not a palindrome
fgetskeeps the Enter key as'\n'at the end. The length loop stops there and the next line replaces it with'\0'. If you skip this, the last character compared is'\n'and every string looks like "not a palindrome".- Avoid
gets(). It was removed from the C standard because it cannot limit input length. "Nitin"fails because'N'and'n'are different characters.
Case-insensitive palindrome check
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int is_palindrome_ignore_case(const char str[]) {
int left = 0, right = strlen(str) - 1;
while (left < right) {
if (tolower((unsigned char) str[left]) != tolower((unsigned char) str[right])) {
return 0;
}
left++;
right--;
}
return 1;
}
int main(void) {
printf("Madam: %d\n", is_palindrome_ignore_case("Madam"));
printf("RaceCar: %d\n", is_palindrome_ignore_case("RaceCar"));
printf("Python: %d\n", is_palindrome_ignore_case("Python"));
return 0;
}
Madam: 1
RaceCar: 1
Python: 0
tolower() from ctype.h turns both characters into lowercase before comparing. To
also skip spaces and punctuation, see
check whether a sentence is a palindrome ignoring spaces and case.
The Java version is on check whether a string is a palindrome, and the Python version is on palindrome program in Python.
O(n), with at most n / 2 comparisonsO(1); no reversed copy is madeReview before the interview
Common mistakes
- Setting right = strlen(str) instead of strlen(str) - 1, which compares with the '\0' at the end.
- Not removing the '\n' that fgets keeps, so no input is ever a palindrome.
- Printing "palindrome" inside the loop after the first matching pair, before all pairs are checked.
- Using strrev(), which is not standard C and fails to compile on GCC.
- Comparing strings with == instead of comparing characters. == compares addresses, not text.
Edge cases to test
- Empty string: right is -1, the loop does not run, and it counts as a palindrome.
- Single character: always a palindrome.
- Even length, like "abba": works, the indexes cross in the middle.
- Mixed case, like "Madam": not a palindrome in the basic version; use tolower() to ignore case.
Interview follow-ups
- How would you ignore spaces and punctuation, so "A man, a plan" is checked correctly?
- How would you write the check using recursion?
- How would you find the longest palindromic substring in a string?
Keep practising
Check Whether a String Is a Palindrome
Hints · Dry run · SolutionSolve Similar problemCheck Whether a Sentence Is a Palindrome Ignoring Spaces and Case
Hints · Dry run · SolutionSolve Similar problemPalindrome Program in Python (Number and String)
Hints · Dry run · SolutionSolve Similar problemReverse a String Without Using Built-in Methods
Hints · Dry run · SolutionSolve Related learningLearn C Programming Step By Step
TutorialLearn Related learningWhat Is A Pointer In C
TutorialLearn Related learningC Programming Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartBuilding your programming basics?
CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.
