networking · beginner · ~15 min

Count hextet groups

Tokenise an IPv6 address by ':'.

Challenge

Count the colon-separated groups (hextets) in a fully written IPv6 address.

Task

Implement int count_groups(const char *addr). The address is in full form (no :: compression), so the answer is simply the number of colons plus 1.

Input

  • addr: an IPv6 address string in full (uncompressed) form.

Output

The count of :-separated groups.

Example

count_groups("2001:db8:0:0:0:0:0:1")  ->  8
count_groups("a:b:c")                 ->  3

Edge cases

  • A full IPv6 address has 8 hextets and 7 colons.

Input format

addr: an IPv6 address string in full (uncompressed) form.

Output format

The number of ':'-separated groups (colons + 1).

Constraints

Assume full form with no '::' compression.

Starter code

int count_groups(const char *addr) {
    /* TODO */
    return 0;
}

Common mistakes

Assuming read/write transmits all requested bytes at once; ignoring socket file descriptor cleanup; unhandled endianness conversions.

Edge cases to handle

Short reads/writes; peer closing socket (recv returns 0); signal interruption (EINTR); non-blocking EAGAIN.

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