basics · beginner · ~15 min

Case-insensitive alphanumeric palindrome check

Two-pointer scan with character-class filtering.

Challenge

Check whether a string reads the same forwards and backwards, ignoring case and any non-alphanumeric characters.

Task

Implement int is_palindrome(const char *s) that returns 1 if s is a palindrome and 0 otherwise. Compare only alphanumeric characters (isalnum), skip everything else, and treat upper- and lowercase as equal. Use a two-pointer scan from both ends inward — do not build a cleaned copy.

Input

A NUL-terminated ASCII string s.

Output

Returns 1 if s is a palindrome under those rules, otherwise 0.

Example

is_palindrome("racecar")                          ->   1
is_palindrome("RaceCar")                          ->   1   (case-insensitive)
is_palindrome("A man, a plan, a canal: Panama")   ->   1
is_palindrome("hello")                            ->   0
is_palindrome("")                                 ->   1   (empty, by convention)
is_palindrome(" ,.,. ")                           ->   1   (no alnum chars)

Edge cases

  • The empty string and strings with no alphanumeric characters are palindromes (return 1).
  • A single character is a palindrome.

Rules

  • Single pass with the two-pointer technique; no allocations, no strrev.

Why this matters

Two-pointer scanning is the algorithmic backbone of dozens of string problems. The palindrome check is the smallest example that exercises ASCII case-folding and skipping irrelevant characters — exactly the moves you'll reuse in URL normalisation and CSV repair.

Input format

A NUL-terminated ASCII string s.

Output format

1 if s is a palindrome (case- and punctuation-insensitive), otherwise 0.

Constraints

No allocations, no strrev; use two pointers from both ends.

Starter code

int is_palindrome(const char *s) { /* TODO */ return 0; }

Common mistakes

Allocating a 'cleaned' copy of the string (works but wastes memory). Forgetting that isalnum requires the argument cast to unsigned char to be safe.

Edge cases to handle

Empty string; string of only punctuation; single character.

Complexity

O(strlen(s)).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.