networking · beginner · ~15 min
The v4-vs-v6 family-detect heuristic that real parsers use before calling inet_pton.
Implement int classify_ip_string(const char *s) returning:
4 if the string is a candidate IPv4 (contains a . and no :).6 if it's a candidate IPv6 (contains a :).0 for anything else (empty, NULL, doesn't look like either).This is a family-detect gate; you don't have to validate the address — just classify.
A modern parser must accept both '192.168.1.1' and '::1'. Detecting the family from the string is the lightweight gate before calling inet_pton.
String.
4 / 6 / 0.
No regex; pure character scan.
int classify_ip_string(const char *s) { /* TODO */ (void)s; return 0; }
Matching : inside an IPv4-mapped IPv6 like ::ffff:1.2.3.4 and getting confused — that string IS v6.
NULL, empty, just dots, just colons.
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.