Check a Palindrome String in C and C++
Does the word read the same backwards? Compare characters from both ends and meet in the middle.
- Two indexes moving toward each other
- Comparing mirrored characters
- Stopping at the middle
Compare from both ends
The problem
Is "level" the same backwards? Palindrome checks appear in puzzles, data validation and interviews. Comparing characters from both ends answers it without building a reversed copy.
Think of it like this
Checking a palindrome is like two people walking toward each other from opposite ends of a row of tiles, reading the letter under their feet. If every pair matches until they meet in the middle, the word reads the same both ways.
text[i]🚶 The walker from the lefttext[length - 1 - i]🚶 The walker from the right!=❌ Letters differ: not a palindromei < length / 2🤝 They meet in the middle: stopA palindrome is text that reads the same forwards and backwards, checked by comparing each character with its mirror from the other end.
Important points
text[i]is compared withtext[length - 1 - i], its mirror position.- The loop only runs to
length / 2, because each pair is checked once. - A mismatch sets
palindrometo 0 and stops the loop. - In C++,
std::equalcompares the first half withtext.rbegin(), a reversed view.
When to use it
- String puzzles and interview questions.
- Practicing index arithmetic from both ends.
- Checking symmetric data such as codes or sequences.
When not to use it
- Sentences with spaces, punctuation or mixed case: clean the text first.
- Unicode text where one character may take several bytes.
C and C++ code examples
#include <stdio.h>
#include <string.h>
int main(void) {
const char *text = "level"; size_t length = strlen(text); int palindrome = 1;
// Compare characters mirrored around the center.
for (size_t i = 0; i < length / 2; ++i)
if (text[i] != text[length - 1 - i]) { palindrome = 0; break; }
printf("%s is %sa palindrome\n", text, palindrome ? "" : "not ");
}
level is a palindrome
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string text = "level";
// equal compares the first half with a reversed view.
bool palindrome = std::equal(text.begin(), text.begin() + text.size() / 2, text.rbegin());
std::cout << text << " is " << (palindrome ? "" : "not ") << "a palindrome\n";
}
level is a palindrome
C and C++ comparison
The loop compares the first character with the last, the second with the second-last, and so on. It stops at the middle because each pair only needs one check. Every pair in "level" matches, so it is a palindrome.
C exposes small procedural APIs and makes representation details explicit.
C++ retains the low-level model while adding safer library types and abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add invalid and boundary-value inputs.
- Compile with -Wall -Wextra -Wpedantic.
- Move reusable declarations into a header and implementation file.
Common mistakes
Off-by-one mirror index
text[length - i] reads the '\0' terminator for i = 0.
text[i] != text[length - i]
text[i] != text[length - 1 - i]
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges