basics · beginner · ~15 min

String equality

Compare two strings character by character.

Challenge

Compare two strings for exact equality, without using the library.

Task

Implement int str_equal(const char *a, const char *b) that returns 1 if a and b are identical strings, else 0. Do not call strcmp — compare character by character. No main — the grader calls it.

Input

Two NUL-terminated strings a and b.

Output

Returns 1 if the strings are identical, otherwise 0.

Example

str_equal("abc", "abc")   ->   1
str_equal("abc", "abd")   ->   0
str_equal("ab", "abc")    ->   0     (b is a prefix, not equal)
str_equal("", "")         ->   1

Edge cases

  • Two empty strings are equal.
  • A prefix is not equal to the longer string.

Rules

  • Do not call strcmp — walk both strings yourself.

Input format

Two NUL-terminated strings a and b.

Output format

1 if identical, otherwise 0.

Constraints

Do not call strcmp.

Starter code

int str_equal(const char *a, const char *b) {
    /* TODO */
    return 0;
}

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