basics · beginner · ~15 min

Bounded string length

Bound a scan so a missing NUL can't run off the end.

Challenge

Implement int safe_strlen(const char *s, int max) returning the length of s but scanning at most max bytes (like strnlen).

Starter code

int safe_strlen(const char *s, int max) {
    /* TODO */
    return 0;
}

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