file-handling · intermediate · ~30 min
Per-line classification and aggregate counting in one pass.
Implement void log_count_levels(const char *log, int *info, int *warn, int *err). The log is a multi-line buffer where each line begins with INFO , WARN , or ERROR (or starts otherwise — ignore those lines). Sets the three output counters to the per-level totals.
Log aggregation is the centerpiece of observability. A function that groups counts by log level is the kernel of every dashboard you've ever looked at.
Null-terminated buffer with lines separated by \n. Each line may or may not start with a level tag.
Three counts via output pointers.
One pass. No allocations.
void log_count_levels(const char *log, int *info, int *warn, int *err) { /* TODO */ }
Counting level-tag occurrences anywhere on a line (must be a prefix); double-counting when a line wraps; not handling the last (non-newline-terminated) line.
Empty input. Unknown prefix lines. Multiple consecutive newlines.
O(n) time.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.