cybersecurity · intermediate · ~20 min
Multi-rule validation; stateful character scan.
Validate a username strictly at the input boundary so every downstream layer (filenames, logs, queries) can trust it.
Implement int valid_username(const char *u) that returns 1 if u satisfies all the rules below, else 0.
u: a NUL-terminated candidate username the grader passes.Returns int: 1 if valid, 0 otherwise. A username is valid when:
a-z;_, -, or .;..);valid_username("alice") -> 1
valid_username("a.b-c.d") -> 1
valid_username("Ab") -> 0 (uppercase + too short)
valid_username("a..b") -> 0 (consecutive dots)
valid_username(".alice") -> 0 (leading dot)
valid_username("1abc") -> 0 (leading digit)
Usernames flow through filenames, log lines, SQL queries, and shell commands. Validating them strictly at input-time prevents an enormous class of injection attacks — every subsequent layer can trust the value.
A NUL-terminated candidate username u.
An int: 1 if u passes all the rules, else 0.
No regex — scan the string directly.
int valid_username(const char *u) { /* TODO */ return 0; }
Allowing capital letters (some systems case-fold and create homograph collisions); allowing dots at end (filename-mode username/.config ambiguity); skipping the consecutive-dot check (path traversal vibes).
Empty string. Single dot. 33-char string just over the limit.
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.