Expedia Interview Question
Software Engineer / DevelopersIt is a good idea to check if the input is valid, ie all segments of the ip address are in the interval [0,255]
List<int> passIpAddress(string ip)
{
int end = 0, start=0;
List<int> output = new List<int>();
while(end < ip.Length)
{
start = end;
while (end < ip.Length && ip[end] != '.')
{
end++;
}
end--;
int n = 0;
int num = 0;
for (int i = start; i <= end; i++)
{
num = num * 10;
num = num+ (ip[i] - '0');
}
output.Add(num);
end = end + 2;
}
return output;
}
- Anna September 27, 2011