Strings · beginner
C Program to Check Whether a String is a Palindrome
Check whether a string is a palindrome in C with two indexes moving towards the middle, ignoring case and punctuation with isalnum() and tolower().
- char arrays
- strlen()
- two indexes
- ctype.h
A palindrome reads the same forwards and backwards, such as "level" or "racecar". In C a string is an array of characters ending in '\0', so the natural solution is two indexes: one from the start, one from the end, compared and moved towards the middle.
For sentences, the program skips characters that are not letters or digits and compares letters without regard to case.
The program
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
bool is_palindrome(const char *s) {
size_t left = 0, right = strlen(s);
while (left < right) {
if (!isalnum((unsigned char)s[left])) { left++; continue; }
if (!isalnum((unsigned char)s[right - 1])) { right--; continue; }
if (tolower((unsigned char)s[left]) != tolower((unsigned char)s[right - 1]))
return false;
left++;
right--;
}
return true;
}
int main(void) {
const char *tests[] = {"racecar", "hello", "A man, a plan, a canal: Panama", "12321"};
for (int i = 0; i < 4; i++)
printf("\"%s\" %s\n", tests[i], is_palindrome(tests[i]) ? "is a palindrome" : "is not a palindrome");
return 0;
}"racecar" is a palindrome
"hello" is not a palindrome
"A man, a plan, a canal: Panama" is a palindrome
"12321" is a palindromeHow it works
- left starts at the first character and right one past the last, so s[right - 1] is the last character.
- Characters that are not letters or digits (isalnum is false) are skipped from either end.
- tolower() compares letters case-insensitively. Casting to unsigned char keeps ctype functions safe for any byte value.
- A mismatch returns false at once; if the indexes meet, every pair matched and the string is a palindrome.
Another way: Reverse a copy and compare
For a simple word without spaces, copy the string reversed into a second array and compare with strcmp().
#include <stdio.h>
#include <string.h>
int main(void) {
const char *word = "level";
char reversed[32];
size_t n = strlen(word);
for (size_t i = 0; i < n; i++)
reversed[i] = word[n - 1 - i];
reversed[n] = '\0';
printf("%s -> %s: %s\n", word, reversed, strcmp(word, reversed) == 0 ? "palindrome" : "not a palindrome");
return 0;
}level -> level: palindromeCommon mistakes
- Forgetting the terminating '\0' when building the reversed copy, so printf reads past the end.
- Comparing strings with == instead of strcmp(): == compares addresses, not contents.
- Passing a plain (possibly negative) char to tolower() or isalnum(), which is undefined for bytes above 127.
Try it yourself
- Read a line with fgets() and check it (remove the trailing newline first).
- Find the longest palindromic word in a sentence.
Open the program in the C Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.
Related examples
Browse all 25 C examples, or write your own program in the online C Compiler.