networking · intermediate · ~15 min

Parse a host:port string

Split an endpoint into host and numeric port.

Challenge

Implement int parse_endpoint(const char *s, char *host, int hostsz, int *port) splitting "host:port" into its parts. Return 1 on success, 0 if there is no ':' or the port is invalid.

Starter code

#include <string.h>
#include <stdlib.h>

int parse_endpoint(const char *s, char *host, int hostsz, int *port) {
    /* TODO */
    return 0;
}

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