basics · beginner · ~15 min
Walk a C string to its NUL terminator.
Measure the length of a C string yourself, the way strlen does.
Implement size_t my_strlen(const char *s) that returns the number of characters in s before the terminating NUL byte. Do not call strlen.
A NUL-terminated string s.
Returns the character count (excluding the NUL) as a size_t.
my_strlen("hello") -> 5
my_strlen("") -> 0
my_strlen("a") -> 1
strlen — walk the string yourself.A NUL-terminated string s.
The number of characters before the NUL, as a size_t.
Do not call strlen.
#include <stddef.h>
size_t my_strlen(const char *s) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.