cybersecurity · beginner · ~20 min
Strict prefix matching.
Match rotated log files against a base name — the lookup an incident-response tool uses to find the right archive (app.log.1, app.log.2024-01-15, ...).
Implement int is_rotated_log(const char *name, const char *candidate) that returns 1 if candidate is name itself or a rotation of it, else 0.
A candidate matches when it is either:
name, ORname followed by a literal . followed by at least one more character.name: the base log name (e.g. app.log).candidate: a filename to test.Returns int: 1 if candidate matches the rule above, else 0.
is_rotated_log("app.log", "app.log") -> 1 (exact)
is_rotated_log("app.log", "app.log.1") -> 1
is_rotated_log("app.log", "app.log.2024-01-15.gz") -> 1
is_rotated_log("app.log", "app.log.") -> 0 (nothing after the dot)
is_rotated_log("app.log", "app.log2") -> 0 (no separating dot)
is_rotated_log("app.log", "unrelated") -> 0
app.log.) does not match.app.log2 does not match — the dot separator is required.name cannot match.A common pattern in incident response: scan a directory listing for rotated log files (app.log.1, app.log.2024-01-15, etc.) so you can pull the right archive. Building a simple matcher teaches glob-like patterns the disciplined way.
Two NUL-terminated strings: the base name and a candidate filename.
An int: 1 if candidate is name or a rotation of it, else 0.
name is base log file name; candidate is candidate filename to test.
int is_rotated_log(const char *name, const char *candidate) { /* TODO */ return 0; }
Treating 'app-old.log' as rotation of 'app.log' without dot boundary.
Exact match, name followed by dot and number, prefix match without dot separator.
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.