cybersecurity · intermediate · ~20 min
Multi-rule validation; stateful character scan.
Implement int valid_username(const char *u). Rules:
_, -, ....).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.
Null-terminated string.
0 or 1.
No regex.
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.