A string palindrome in C using string functions is typically checked by reversing a copy of the string with strrev() (available in compilers like Turbo C and some others) and comparing it to the original with strcmp(), or — in fully standard C — by using strlen() from <string.h> to compare characters from both ends toward the middle. If the string reads the same forwards and backwards, like "madam" or "racecar", it is a palindrome.
What Is a Palindrome String?
A palindrome is a sequence of characters that reads identically in both directions. Classic single-word examples in English include level, noon, civic, and radar. Numbers can be palindromes too, such as 12321. In C, a string is stored as an array of characters terminated by a null character ('\0'), and every palindrome-checking method must respect that terminator.
A common beginner misconception is that the comparison should ignore spaces, punctuation, and capitalization automatically. It does not. In plain C, "Madam" and "madam" are different strings because strcmp() compares raw character codes, so 'M' and 'm' are unequal. If you want case-insensitive matching, you must normalize the string first with tolower() from <ctype.h> or use strcasecmp() / _stricmp() where available.
Method 1: Using strrev() and strcmp()
The simplest-looking approach copies the input, reverses the copy, and compares. Note carefully: strrev() is not part of the ISO C standard library. It exists in older compilers such as Turbo C and in some implementations (for example as _strrev() in Microsoft's C runtime), but GCC on Linux does not provide it. If your course or textbook uses it, you are likely targeting one of those environments.
#include <stdio.h>
#include <string.h>
int main() {
char str[100], copy[100];
printf("Enter a string: ");
gets(str); /* use fgets in modern code */
strcpy(copy, str); /* keep the original intact */
strrev(copy); /* non-standard function */
if (strcmp(str, copy) == 0)
printf("\"%s\" is a palindrome.\n", str);
else
printf("\"%s\" is not a palindrome.\n", str);
return 0;
}
The key steps are:
- Copy the original string so reversing does not destroy it —
strcpy(). - Reverse the copy —
strrev(). - Compare original and reversed copy —
strcmp()returns 0 only when they match exactly.
Method 2: Standard C with strlen() Only
For portable code, use only strlen() and compare characters from both ends toward the center. This avoids the non-standard function entirely and is what most production code does:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; /* trim trailing newline */
int len = strlen(str);
int isPalindrome = 1;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
isPalindrome = 0;
break;
}
}
if (isPalindrome)
printf("Palindrome\n");
else
printf("Not a palindrome\n");
return 0;
}
This version needs only one pass over half the string, allocates no extra buffer, and compiles with any conforming C compiler.
How the Comparison Actually Works
strcmp(s1, s2) walks both strings character by character and returns 0 at the first point where they differ completely matched to the end. Both strings must be null-terminated; a missing '\0' causes undefined behavior, which in practice means garbage comparisons or crashes. Similarly, strrev() and strlen() rely on finding the terminator, so never pass them a raw character array you built by hand without terminating it.
Common Problems and Fixes
- gets() is unsafe and removed from the C11 standard. It has no bounds checking and can overflow the buffer. Use
fgets()instead, then strip the newline it leaves behind. - strrev() fails to compile on GCC/Clang. That is expected because it is non-standard. Either write your own reverse loop or use Method 2.
- Comparing with == instead of strcmp().
if (str == copy)compares pointer addresses, never the contents, and is essentially always false for two different arrays. - Case sensitivity. "Madam" will fail the test unless you lowercase both strings first.
- Spaces and punctuation. "Never odd or even" fails a naive check because of spaces. Phrase-level palindrome checkers filter out non-alphabetic characters before comparing.
Comparing the Two Approaches
- strrev() + strcmp(): shortest to write, matches many textbook exercises, but non-portable and uses an extra buffer the same size as the input.
- Two-pointer loop with strlen(): fully standard, no extra memory, and faster in principle since it stops at the first mismatch. This is the version to use in real projects and interviews.
Performance rarely matters for strings a user types at a console — both methods complete in microseconds. Choose based on portability and correctness, not speed.
Handling Sentences and Mixed Case
To check phrases like "A man, a plan, a canal: Panama", preprocess the input first:
- Copy only alphabetic or numeric characters into a new buffer (test with
isalnum()from<ctype.h>). - Convert each copied character with
tolower(). - Run either palindrome method on the cleaned string.
This cleaning step is why palindrome checkers for natural-language text look more complicated than the textbook examples.
Frequently Asked Questions
Is strrev() a standard C function?
No. strrev() is a vendor extension found in compilers like Turbo C and some Windows runtimes, but it is not defined by the ISO C standard and is absent from glibc, so it will not compile on typical Linux GCC setups.
Why does strcmp() return 0 for equal strings?
strcmp() is designed to return a negative, zero, or positive value indicating lexicographic order, not a true/false flag. Zero means equal; treating the return value as a boolean (as in if (strcmp(a,b))) actually tests for inequality and is a classic beginner bug.
Can I check a number string like 12321 the same way?
Yes. If the number is read as a string, the same code works unchanged because the digits are just characters. Alternatively, you can reverse the integer arithmetically with modulo and division, but the string approach is simpler and avoids overflow when reversing large numbers.
Why is gets() used in old palindrome examples?
Older textbooks predate the removal of gets() from the standard. It reads a whole line without any length limit, which makes it dangerously unsafe. Modern code should always use fgets() with a buffer size.
Does the palindrome check need the string.h header?
Only if you use its functions. strlen(), strcmp(), and strcpy() are declared in <string.h>. A hand-written loop that finds the terminator itself needs no string functions at all, though using strlen() is clearer.