networking · beginner · ~15 min

Is this a localhost name?

Recognise local-only test targets.

Challenge

Recognise the spellings that all refer to the local machine.

Task

Implement int is_localhost(const char *host).

Input

  • host: a NUL-terminated host string.

Output

Return 1 if host is exactly "localhost", "127.0.0.1", or "::1"; else 0.

Example

is_localhost("localhost")    ->  1
is_localhost("127.0.0.1")    ->  1
is_localhost("::1")          ->  1
is_localhost("example.com")  ->  0

Input format

host: a NUL-terminated host string.

Output format

1 if host is "localhost", "127.0.0.1", or "::1"; else 0.

Constraints

Exact match against the three loopback spellings.

Starter code

#include <string.h>

int is_localhost(const char *host) {
    /* TODO */
    return 0;
}

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