To check a palindrome string in C, compare the first character with the last, the second with the second to last, and so on towards the middle. If every pair matches, the string is a palindrome; the first mismatch means it is not. The check runs in O(n) time with no extra memory, and it is a common interview question.
This guide builds a basic version, then a more realistic one that ignores case, spaces and punctuation, and then lists the edge cases to test.
A palindrome reads the same forwards and backwards, such as level or madam. Use one index from the start and one from the end, and move them inward.
The early return for lengths 0 and 1 is not only an optimisation. With an empty string, len - 1 on the unsigned size_t would wrap around to a huge value, so the guard prevents a serious bug. The function takes const char * because it only reads the text, so it works with string literals too.
Real phrases such as A man, a plan, a canal: Panama are palindromes only when you skip non-letters and ignore case. Use isalnum and tolower from ctype.h, and skip unwanted characters from both ends.
Here right is an exclusive end, so the last candidate character is at right - 1. This form avoids unsigned underflow on empty or all-punctuation strings. The cast to unsigned char is required, because passing a negative char value to isalnum or tolower is undefined behaviour.
Another approach copies the string, reverses it and compares with strcmp. It works, but it needs O(n) extra memory and two passes, so the two-index method is better.
The fixed buffer shows the downside: you must handle long inputs. For this reason interviewers prefer the in-place check.
| Input | Expected result | Why it matters |
|---|---|---|
| Empty string | Palindrome | Guards the unsigned len - 1 |
| One character | Palindrome | Loop never runs |
abba (even length) | Palindrome | Indexes meet between characters |
level (odd length) | Palindrome | Middle character is not compared |
Madam with a plain compare | Not a palindrome | M and m differ unless case is folded |
race a car | Not a palindrome | Mismatch found after skipping spaces |
Each character is compared at most once and only half the string is visited, so the time is O(n) and the extra space is O(1) for the two-index versions. The code works on bytes, so it is correct for ASCII. For UTF-8 text with multi-byte characters, a byte comparison would give wrong answers and you need a Unicode-aware library.
By the usual definition, yes, since it reads the same in both directions. Most programs return true for it, and you should state that choice.
Convert it to text with snprintf and use the same check, or reverse its digits arithmetically with % and /. Beware of overflow when reversing large integers.
The ctype functions accept values of unsigned char or EOF. A negative char is undefined behaviour, so the cast makes the call safe.
Use a loop with two indexes. It needs no copy and stops at the first mismatch.
Compare characters from both ends moving inward and return false on the first difference. Guard empty strings against unsigned underflow, fold case with tolower and skip non-alphanumeric characters when checking phrases, and remember that the byte-based method is correct for ASCII only.