C++ Palindrome Program: Strings and Numbers
A palindrome reads the same forwards and backwards — words like “level” and “radar”, or numbers like 121 and 1331. Writing a palindrome checker is a great exercise because it teaches you to walk through data from both ends and think carefully about comparisons.
The Two-Pointer Idea
The cleanest way to check a string is to compare the first character with the last, the second with the second-to-last, and so on. We use two indices: one starting at the front, one at the back. They move toward each other, and if any pair disagrees, the string is not a palindrome.
#include <iostream>
#include <string>
bool isPalindrome(const std::string& s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) return false; // mismatch found
left++;
right--;
}
return true; // all pairs matched
}
int main() {
std::cout << std::boolalpha;
std::cout << isPalindrome("radar") << "\n"; // true
std::cout << isPalindrome("hello") << "\n"; // false
return 0;
}
This is efficient because we only walk through half the string and never allocate a second copy. We pass the string by const reference (const std::string&) to avoid copying the whole thing into the function.
Method 2: Reverse and Compare
A more beginner-obvious approach builds a reversed copy and checks whether it equals the original:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string word = "level";
std::string reversed = word;
std::reverse(reversed.begin(), reversed.end());
if (word == reversed)
std::cout << word << " is a palindrome.\n";
else
std::cout << word << " is not a palindrome.\n";
return 0;
}
This is shorter to write thanks to std::reverse from <algorithm>, but it uses extra memory for the reversed copy. For most beginner programs that trade-off is perfectly fine.
Checking a Number Palindrome
Numbers need a different trick because you can’t index digits directly. Instead, we rebuild the number in reverse using % 10 to grab the last digit and / 10 to drop it:
#include <iostream>
bool isPalindrome(int n) {
if (n < 0) return false; // negatives like -121 aren't palindromes
int original = n;
int reversed = 0;
while (n > 0) {
int digit = n % 10; // last digit
reversed = reversed * 10 + digit;
n /= 10; // remove last digit
}
return original == reversed;
}
int main() {
std::cout << std::boolalpha;
std::cout << isPalindrome(121) << "\n"; // true
std::cout << isPalindrome(123) << "\n"; // false
return 0;
}
Each loop pass shifts reversed left by one decimal place and appends the next digit. When the original number is exhausted, we compare the two values.
Common Pitfalls
Case sensitivity trips people up: "Radar" fails a naive check because 'R' and 'r' differ. Convert to one case first if you want case-insensitive matching. For numbers, remember that negatives can’t be palindromes because of the minus sign. And always make sure your loop condition is left < right, not left <= right, so the exact middle character isn’t compared with itself unnecessarily.
Related Articles
- C++ String Handling — methods, length, and indexing
- C++ Loops Tutorial — while and for loops explained
- C++ Conditionals Tutorial — making decisions with if
- C++ Functions Tutorial — writing reusable checks
- C++ Sort Algorithm — more std::algorithm tools
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.