Beginner C/C++ Exercises

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.

  • Beginner
  • 2 min read
  • 2 runnable examples
What you'll learn
  • Two indexes moving toward each other
  • Comparing mirrored characters
  • Stopping at the middle
"level"
l0 ↔ 4
e1 ↔ 3
vmiddle
e3
l4
l = l and e = e, so "level" is a palindrome.

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.

From two walkers to code
text[i]🚶 The walker from the left
text[length - 1 - i]🚶 The walker from the right
!=❌ Letters differ: not a palindrome
i < length / 2🤝 They meet in the middle: stop
Compare mirrored characters until the middle.
In one sentence

A palindrome is text that reads the same forwards and backwards, checked by comparing each character with its mirror from the other end.

Important points

  1. text[i] is compared with text[length - 1 - i], its mirror position.
  2. The loop only runs to length / 2, because each pair is checked once.
  3. A mismatch sets palindrome to 0 and stops the loop.
  4. In C++, std::equal compares the first half with text.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

C
Run code →
main.c
#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 ");
}
Get a Run button for this example Result of running the code
level is a palindrome
C++
Run code →
main.cpp
#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";
}
Get a Run button for this example Result of running the code
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

C exposes small procedural APIs and makes representation details explicit.

C++

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.

Problem
text[i] != text[length - i]
Fix
text[i] != text[length - 1 - i]

Finished this lesson?

Mark it as learned to track your progress.

Next: Sum Array Values →

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