file-handling · intermediate · ~15 min
Track a line counter while scanning.
Find where the first error appears in a log, reported as a line number.
Implement int first_error_line(const char *log) that returns the 1-based line number of the first line containing "ERROR".
log: a multi-line string, lines separated by '\n'.
Return the 1-based line number of the first line containing "ERROR", or -1 if no line does.
first_error_line("a\nb ERROR\nc\n") -> 2
first_error_line("ERROR here\n") -> 1
first_error_line("all good\n") -> -1
log: multi-line string (lines split by '\n').
int 1-based line number of the first "ERROR" line, or -1 if none.
Count lines from 1; match "ERROR" within a single line.
#include <string.h>
int first_error_line(const char *log) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.