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()
?
strnlen(s, maxlen)
function returnsstrlen(s)
, if that is less thanmaxlen
, ormaxlen
if there is no null byte ('\0') among the firstmaxlen
bytes pointed to bys
.