networking · beginner · ~15 min

Valid hostname character?

Whitelist the legal hostname characters.

Challenge

Decide whether a single character is allowed in a hostname — the per-character allow-list you apply before a DNS lookup.

Task

Implement int valid_hostname_char(char c).

Input

  • c: a single character.

Output

Return 1 if c is a letter (a-z/A-Z), a digit (0-9), a hyphen (-), or a dot (.); else 0.

Example

valid_hostname_char('a')  ->  1
valid_hostname_char('9')  ->  1
valid_hostname_char('-')  ->  1
valid_hostname_char('/')  ->  0

Input format

c: a single character.

Output format

1 if c is a letter, digit, '-', or '.'; else 0.

Constraints

Allowed set: a-z, A-Z, 0-9, '-', '.'.

Starter code

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.