networking · beginner · ~15 min
Whitelist the legal hostname characters.
Decide whether a single character is allowed in a hostname — the per-character allow-list you apply before a DNS lookup.
Implement int valid_hostname_char(char c).
c: a single character.Return 1 if c is a letter (a-z/A-Z), a digit (0-9), a hyphen (-), or a dot (.); else 0.
valid_hostname_char('a') -> 1
valid_hostname_char('9') -> 1
valid_hostname_char('-') -> 1
valid_hostname_char('/') -> 0
c: a single character.
1 if c is a letter, digit, '-', or '.'; else 0.
Allowed set: a-z, A-Z, 0-9, '-', '.'.
int valid_hostname_char(char c) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.