How does strnlen() internally work in C++?

c++, string

Solution

But how would you implement `strnlen()`?

Like this:

size_t strnlen(const char *s, size_t max_len)
{
    size_t i = 0;
    for(; (i < max_len) && s[i]; ++i);
    return i;
}

Problem

I'm confused how `strnlen` could work in C++ with strings that weren't null terminated, as I 'm not sure how it computes the size. The `strlen()` implementation is easy: ``` size_t strlen(char *s) { size_t sz; while(*s++ != '\0') { ++sz; } return sz; } ``` But how would you implement `strnlen()`?

Original source