basics · beginner · ~15 min

Has a terminator in range?

Verify a buffer is NUL-terminated before treating it as a string.

Challenge

Implement int has_nul_within(const char *buf, int n) returning 1 if a NUL byte appears within the first n bytes of buf, else 0. This is how you check a buffer is a safe C string before using it.

Starter code

int has_nul_within(const char *buf, int n) {
    /* TODO */
    return 0;
}

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