/**
* 验证IP地址
* @param IP string字符串 一个IP地址字符串
* @return string字符串
*/
char* ifIPv4(char* Ip);
char* ifIPv6(char* Ip);
char* solve(char* IP ) {
int Temp = 0;
while (IP[Temp] != '.' && IP[Temp] != ':')
Temp++;
if (IP[Temp] == '.')
return ifIPv4(IP);
else if (IP[Temp] == ':')
return ifIPv6(IP);
return "Error";
}
char* ifIPv4(char* Ip) {
int Index = 0;
int Val;
int CountforDot = 0; //'.'的个数, '\0'也看作为一个'.'
while (CountforDot < 4) {
if (Ip[Index] == '0' && Ip[Index + 1] != '.' && Ip[Index + 1] != '\0')
return "Neither";
if(Ip[Index]=='.'||Ip[Index]=='\0')
return "Neither";
Val = 0;
while (Ip[Index] != '.' && Ip[Index] != '\0') {
Val = Val * 10 + Ip[Index] - '0';
Index++;
}
CountforDot++;
if (Val > 255)
return "Neither";
Index++;
}
if(Ip[Index-1]=='\0')
return "IPv4";\
else
return "Neither";
}
char* ifIPv6(char* Ip) {
int Index = 0;
int Len;
int CountforDot = 0; //':'的个数, '\0'也看作为一个':'
while (CountforDot < 8) {
if(Ip[Index]==':'||Ip[Index]=='\0')
return "Neither";
Len = 0;
while (Ip[Index] != ':' && Ip[Index] != '\0') {
if(('0'<=Ip[Index]&&Ip[Index]<='9') || ('a'<=Ip[Index]&&Ip[Index]<='f') || ('A'<=Ip[Index]&&Ip[Index]<='F') ){
Len++;
Index++;
}
else
return "Neither";
}
CountforDot++;
if (Len > 4)
return "Neither";
Index++;
}
if(Ip[Index-1]=='\0')
return "IPv6";\
else
return "Neither";
}