basics · beginner · ~15 min

Case-insensitive alphanumeric palindrome check

Two-pointer scan with character-class filtering.

Challenge

Implement int is_palindrome(const char *s) that returns 1 if s reads the same forwards and backwards (ignoring case, ignoring all non-alphanumeric characters), or 0 otherwise.

Examples

is_palindrome("racecar")               -> 1
is_palindrome("RaceCar")               -> 1
is_palindrome("A man, a plan, a canal: Panama") -> 1
is_palindrome("hello")                 -> 0
is_palindrome("")                      -> 1   // empty is a palindrome by convention
is_palindrome(" ,.,. ")                -> 1   // all-non-alnum collapses to empty

Constraints

  • Single pass; two-pointer technique.
  • No allocations.

Edge cases

  • Empty string.
  • Strings with only non-alphanumeric characters.
  • Mixed case: "Aa" is a palindrome (case-insensitive).

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

Null-terminated ASCII string.

Output format

1 (palindrome) or 0.

Constraints

No allocations; no strrev.

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.