Strings In Depth12 / 18
Up next

Length by pointer walk

#include <stdio.h>

size_t my_strlen(const char *s) {
    const char *p = s;
    while (*p != '\0') {
        p++;
    }
    return (size_t)(p - s);
}

int main(void) {
    printf("%zu\n", my_strlen("counted"));
    return 0;
}
What you're solving

subtract pointers to get the count