废话不多说,上代码
bool isCurrectIP(char *ip) { if (ip == NULL) { return false; } char temp[4]; int count = 0; while (true) { int index = 0; while (*ip != ' ' && *ip != '.' && count < 4) { temp[index++] = *ip; ip++; } if (index >= 4) { return false; } temp[index] = ' '; int num = atoi(temp); if (!(num >= 0 && num <= 255)) { return false; } count++; if (*ip == ' ') { if (count == 4) { return true; } else { return false; } } else { ip++; } } }