cybersecurity · intermediate · ~15 min
Enforce an allowlist + length on input.
Validate a username with a strict character allowlist and a length bound.
Implement int valid_username(const char *s) that returns 1 if s is acceptable, else 0.
s is valid when it is 1..32 characters long and every character is a letter, digit, or underscore.
s: a NUL-terminated candidate username the grader passes.Returns int: 1 if valid, else 0.
valid_username("alice_99") -> 1
valid_username("") -> 0 (empty)
valid_username("a b") -> 0 (space not allowed)
[A-Za-z0-9_] rejects the whole string.A NUL-terminated candidate username s.
An int: 1 if s is 1..32 chars of [A-Za-z0-9_], else 0.
Allowlist [A-Za-z0-9_]; length 1..32; empty is invalid.
int valid_username(const char *s) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.