Beginner C/C++ Exercises

Check a Palindrome

A palindrome is unchanged when its character order is reversed.

What is Check a Palindrome?

A palindrome is unchanged when its character order is reversed.

Check whether a word reads the same in both directions.

Important points

  • Compile with warnings enabled and fix every warning.
  • Know the lifetime and type of every value.
  • Validate input and keep array bounds explicit.

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 ");
}
Expected output
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";
}
Expected output
level is a palindrome

C and C++ comparison

Compare mirrored positions and stop at the middle because every pair is checked once.

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.