Interview Questions

Given a IP address as input, validate the ip address

Software QA/Tests Interview Questions from Microsoft


(Continued from previous question...)

Given a IP address as input, validate the ip address

Question:
Given a IP address as input, validate the ip address.
ValidateIp(string inpIP)


maybe an answer:


Here's java implementation:
public static boolean validateIP(String ip) {

if(ip==null || ip.endsWith("."))
return false;

String[] tokens = ip.split("\\.");
System.out.println("Length " + tokens.length);

if(tokens.length != 4)
{
return false;
}


for(String block: tokens)
{
int val = 0;
try{
val = Integer.parseInt(block);

}catch(NumberFormatException nfe) {
nfe.printStackTrace();
return false;
}

if(val < 0 || val > 255)
return false;
}
return true;
}

(Continued on next question...)

Other Interview Questions