-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsIPV_4address.java
More file actions
30 lines (27 loc) · 1005 Bytes
/
IsIPV_4address.java
File metadata and controls
30 lines (27 loc) · 1005 Bytes
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
/*An IP address is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. There are two versions of the Internet protocol, and thus two versions of addresses. One of them is the IPv4 address.
Given a string, find out if it satisfies the IPv4 address naming rules.*/
boolean isIPv4Address(String s) {
int dot=0,i;
for(i=0;i<s.length();i++)
{
if(s.charAt(i)=='.')
dot++;
}
if(dot!=3)
return false;
for(i=0;i<s.length();i++)
{
String g="";
while(i<s.length()&&s.charAt(i)!='.')
{
boolean x=Character.isDigit(s.charAt(i));
if(!x)
return false;
g=g+s.charAt(i);
i++;
}
if(g.isEmpty() || g.length()>3 || Integer.valueOf(g)<0 || Integer.valueOf(g)>255 || (g.length()>1 && g.charAt(0)=='0'))
return false;
}
return true;
}