basics · beginner · ~15 min

Palindrome check

Compare characters with two converging pointers.

Challenge

Decide whether a string reads the same forwards and backwards.

Task

Implement int is_palindrome(const char *s) that returns 1 if s is a palindrome (identical when reversed), else 0. Use two indices converging from each end, comparing characters. Compare characters exactly — no case-folding or skipping. No main — the grader calls it.

Input

A NUL-terminated string s.

Output

Returns 1 if s is a palindrome, otherwise 0.

Example

is_palindrome("racecar")   ->   1
is_palindrome("aa")        ->   1
is_palindrome("abc")       ->   0
is_palindrome("")          ->   1

Edge cases

  • The empty string is a palindrome (returns 1).
  • A single character is a palindrome.

Input format

A NUL-terminated string s.

Output format

1 if s is a palindrome, otherwise 0.

Constraints

Compare characters exactly; the empty string is a palindrome.

Starter code

#include <string.h>

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

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