networking · intermediate · ~15 min

Is the hostname well-formed?

Validate a whole hostname.

Challenge

Implement int hostname_valid(const char *h) returning 1 if h is non-empty and every character is a valid hostname character (letters, digits, '-', '.'), else 0.

Starter code

int hostname_valid(const char *h) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.