Code of the Day
BeginnerArrays and strings

Lab: Palindrome checker

Write a C program that checks whether a string is a palindrome — practising array iteration, string manipulation, and careful null-terminator handling.

Lab · optionalCBeginner25 min
Recommended first

A palindrome reads the same forwards and backwards: "racecar", "level", "madam". Checking for palindromes is a classic exercise that requires iterating over a string from both ends simultaneously — a useful pattern in many problems.

Goal

Write palindrome.c with a function:

int is_palindrome(const char *str);

Returns 1 if str is a palindrome, 0 otherwise. Then test it with several words in main.

Expected output:

"racecar" is a palindrome
"hello" is not a palindrome
"level" is a palindrome
"A" is a palindrome
"" is a palindrome
"abba" is a palindrome

Approach

The two-pointer technique: start with left = 0 and right = strlen(str) - 1. While left < right, compare str[left] and str[right]. If they differ, return 0. Otherwise, advance left and retreat right.

"racecar"
 ^     ^   r == r, advance
  ^   ^    a == a, advance
   ^ ^     c == c, advance
    ^      left == right, done -- palindrome

Edge cases to handle

  • Empty string: palindrome (no characters differ).
  • Single character: palindrome.
  • Even vs. odd length: works with the same algorithm.

Extension: case-insensitive check

Extend your function to treat "Racecar" and "RACECAR" as palindromes:

int is_palindrome_ci(const char *str) {
    int left = 0;
    int right = (int)strlen(str) - 1;
    while (left < right) {
        if (tolower((unsigned char)str[left]) !=
            tolower((unsigned char)str[right])) {
            return 0;
        }
        left++;
        right--;
    }
    return 1;
}

Include <ctype.h> for tolower.

Extension: ignore non-alphabetic characters

"A man a plan a canal Panama" is a palindrome if you ignore spaces and punctuation. Write a version that strips non-alphabetic characters before checking.

Worked solution

#include <stdio.h>
#include <string.h>

int is_palindrome(const char *str) {
    int left = 0;
    int right = (int)strlen(str) - 1;
    while (left < right) {
        if (str[left] != str[right]) {
            return 0;
        }
        left++;
        right--;
    }
    return 1;
}

void check(const char *word) {
    printf("\"%s\" is%s a palindrome\n",
           word, is_palindrome(word) ? "" : " not");
}

int main(void) {
    check("racecar");
    check("hello");
    check("level");
    check("A");
    check("");
    check("abba");
    return 0;
}

Compile and run:

gcc -Wall -Wextra palindrome.c -o palindrome
./palindrome

What you practised

  • Two-pointer iteration over a string
  • Using strlen to find string bounds
  • Handling edge cases (empty string, single character)
  • const char * parameters for read-only string access

You have now completed the Beginner tier of the C track. The Intermediate tier begins with Pointers — the feature that makes C both uniquely powerful and uniquely dangerous.

Finished reading? Mark it complete to track your progress.

On this page