file-handling · intermediate · ~15 min
Iterate lines and test each for a token.
Count how many log lines mention a given level, by testing each line for a substring.
Implement int count_level(const char *log, const char *level) that returns how many lines of log contain level as a substring.
log: a multi-line string, lines separated by '\n'.level: the token to look for (e.g. "ERROR").Return the number of lines containing level.
log = "INFO ok\nERROR bad\nERROR worse\nWARN hmm\n"
count_level(log, "ERROR") -> 2
count_level(log, "INFO") -> 1
count_level(log, "DEBUG") -> 0
log: multi-line string (lines split by '\n'). level: token to find.
int number of lines containing level.
Test each line independently so matches don't span newlines.
#include <string.h>
int count_level(const char *log, const char *level) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.