cybersecurity · intermediate · ~15 min · safe pentest lab

Count printable runs (strings)

Count runs of printable bytes of a minimum length — the heart of the `strings` tool.

Challenge

The strings utility surfaces embedded text in a binary. Implement its counter:

int count_printable_runs(const unsigned char *buf, size_t n, int minlen);

Count maximal runs of printable ASCII (0x20-0x7e) whose length is at least minlen.

Input format

buf of n bytes and a minlen (>=1; values <1 are treated as 1).

Output format

Number of qualifying runs.

Constraints

Non-printable bytes end a run.

Starter code

#include <stddef.h>
/* Count runs of printable ASCII (0x20..0x7e) whose length is >= minlen (>=1). */
int count_printable_runs(const unsigned char *buf, size_t n, int minlen){ (void)buf;(void)n;(void)minlen; return 0; }

Common mistakes

Forgetting to count the final run after the loop; counting runs shorter than minlen.

Edge cases to handle

A run at the very end of the buffer still counts; empty buffer returns 0.

Background lessons

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