pointers-memory · intermediate · ~15 min

Implement strstr

Nested pointer iteration; corner cases of empty inputs.

Challenge

Implement const char *my_strstr(const char *haystack, const char *needle) that returns a pointer to the first occurrence of needle in haystack, or NULL if not found. An empty needle returns haystack.

Starter code

#include <stddef.h>

const char *my_strstr(const char *haystack, const char *needle) {
    /* TODO */
    return NULL;
}

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