-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidate ip.cpp
85 lines (70 loc) · 1.87 KB
/
Validate ip.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Solution {
public:
const string validIPv6Chars = "0123456789abcdefABCDEF";
bool check(string s,int c)
{
if(c==3)
{
if(s[0]=='0' && s.length()>1)
return false;
for(int i = 0 ; i<s.length();i++)
{
if(!isdigit(s[i]))
return false;
}
stringstream geek(s);
int x = 0;
geek >> x;
if(x>255 || x<0||s.length()==0)
return false;
return true;
}
if(s.length()==0|| s.length()>4)
return false;
for(int i = 0 ; i<s.length();i++)
{
if (validIPv6Chars.find(s[i]) == string::npos)
return false;
}
return true;
}
string validIPAddress(string IP) {
int i = 0 ;
string s = "";
int count = 0;
for(int i = 0 ; IP[i]!='\0';i++)
{
if(IP[i]=='.'||IP[i]==':')
{
count++;
}
}
if(count!=7 && count !=3)
{
return "Neither" ;
}
for(int i = 0 ; IP[i]!='\0';i++)
{
if(IP[i]=='.'||IP[i]==':')
{
if(!check(s,count))
{
return "Neither" ;
}
s = "";
}
else
s+=IP[i];
}
if(!check(s,count))
{
return "Neither" ;
}
if(count==3)
return "IPv4";
else if(count==7)
return "IPv6";
else
return "Neither" ;
return 0; }
};