-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path468.cpp
More file actions
55 lines (52 loc) · 1.78 KB
/
468.cpp
File metadata and controls
55 lines (52 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
bool isNumber(char c) {
if (c >= '0' && c <= '9') return true;
return false;
}
bool isHex(char c) {
if ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) return true;
return false;
}
string validIPAddress(string queryIP) {
int dotCnt = 0;
int colonCnt = 0;
int numberCnt = 0;
int hexCnt = 0;
for (auto c : queryIP) {
if (c == '.') dotCnt++;
else if (c == ':') colonCnt++;
else if (isNumber(c)) numberCnt++;
else if (isHex(c)) hexCnt++;
else return "Neither";
}
if (dotCnt > 0 && colonCnt > 0) return "Neither";
if (dotCnt > 0) {
if (dotCnt != 3) return "Neither";
if (hexCnt) return "Neither";
stringstream ss(queryIP);
string token;
if (queryIP.back() == '.') return "Neither";
while (getline(ss, token, '.')) {
if (token.size() == 0 || token.size() > 4) return "Neither";
int num = stoi(token);
if (num == 0 && token.size() > 1) return "Neither";
if (num < 0 || num > 255) return "Neither";
if (to_string(num).size() != token.size()) return "Neither";
}
return "IPv4";
}
if (colonCnt > 0) {
if (queryIP.back() == ':') return "Neither";
if (colonCnt != 7) return "Neither";
stringstream ss(queryIP);
string token;
while (getline(ss, token, ':')) {
if (token.size() == 0) return "Neither";
if (token.size() > 4) return "Neither";
}
return "IPv6";
}
return "Neither";
}
};