linux-sysprog · intermediate · ~15 min

Count env vars with prefix

Walk the process environment.

Challenge

Implement int env_count_with_prefix(const char *prefix) returning the number of environment variables whose name starts with prefix. Use the global environ array.

Starter code

#include <unistd.h>
#include <string.h>
extern char **environ;
int env_count_with_prefix(const char *prefix) {
    /* TODO */
    return 0;
}

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