basics · intermediate · ~15 min

Implement strlen

Walk a pointer through a NUL-terminated string and count the steps.

Challenge

Write strlen() from scratch

strlen is the canonical "rewrite a libc function" exercise. It's only one line of real code, but doing it correctly teaches you C's two foundational ideas: pointer arithmetic, and that strings are NUL-terminated.

Task

Implement:

size_t my_strlen(const char *s);

It must return the number of bytes in s BEFORE the terminating '\0'. No use of strlen from the standard library.

Function signature

size_t my_strlen(const char *s);

Input

A NUL-terminated string.

Output

The number of characters before the '\0', as a size_t. Empty string → 0.

Rules

  • Don't call strlen, strnlen, or anything in <string.h> that you didn't write.
  • Don't read past the '\0' — your function would read forever on a malformed string.
  • s is guaranteed non-NULL by the harness, but defensive code returns 0 for NULL anyway.

Examples

Input Returns
"hello" 5
"" 0
"a" 1
"hello, world" 12

Edge cases

  • Empty string: return 0.
  • Strings with embedded NUL would terminate early (that's correct — C strings stop at the first '\0').
  • Very long strings (>1MB): still works — no allocation, linear scan.

Hints

  1. Conceptual: a C string ends at the first byte equal to 0 (the literal byte, not the character '0'). Loop until you find one.
  2. Implementation: const char *p = s; while (*p) p++; return p - s; — pointer subtraction gives you the count.
  3. Common bug: checking *p != '\0'. That works, but while (*p) is the same thing because '\0' is zero, which is falsy in C. Both are right; the second is idiomatic.

Common mistakes

  • Using a for (int i = 0; s[i] != '\0'; i++) and returning i. Correct, but slower idiomatic style than pointer arithmetic.
  • Returning int instead of size_t. The standard strlen returns size_t.
  • Forgetting that '\0' is the byte 0, not the character '0' (ASCII 48).

Learning connection

This is your first taste of:

  • Pointer arithmetic (p - s gives a difference in elements, here in bytes).
  • The C string convention (NUL-terminated). After this, every other <string.h> function (strcmp, strcat, strchr) reads exactly the same way.

Why this matters

The smallest possible exercise that uses pointers, the string convention, AND pointer arithmetic. Get all three at once.

Input format

A pointer to a NUL-terminated string.

Output format

size_t — the byte count before the NUL.

Constraints

No <string.h> calls. Don't read past the NUL.

Starter code

#include <stddef.h>

size_t my_strlen(const char *s) {
    /* TODO */
    return 0;
}

Common mistakes

Returning int instead of size_t. Using s[i] index notation when pointer arithmetic is the idiom. Reading past the NUL.

Edge cases to handle

Empty string → 0. Embedded NUL → terminates at the first one. Very long strings → linear scan, fine.

Complexity

O(n) where n is the string length. Cannot be faster in the general case — you must touch each byte.

Background lessons

Up next

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