Amazon Interview Question
Software Engineer / DevelopersTeam: Instant Video
Country: United States
Interview Type: Phone Interview
I think this can be easily done using bitwise operators
perl:
#!/usr/bin/perl
my ($a,$b,$c,$d) = split(/\./,"10.1.10.12");
print (($a<<24) | ($b<<16) | ($c<<8) | $d);
c:
#include<stdio.h>
#include<string.h>
int main()
{
unsigned ans=0;
char input[] = "10.1.10.12";
char *tok = strtok(input,".");
int i;
char delims[] = ".";
for(i=3;tok!=NULL;i--)
{
ans |= atoi(tok)<<(8*i);
tok = strtok(NULL,".");
}
printf("%u\n",ans);
}
public double IPAddressToNumber(string IPaddress)
{
int i;
string [] arrDec;
double num = 0;
if (IPaddress == "")
{
return 0;
}
else
{
arrDec = IPaddress.Split('.');
for(i = arrDec.Length - 1; i >= 0 ; i --)
{
num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
}
return num;
}
}
#include <stdio.h>
#define STR1 "192.168.72.46"
#define STR2 ".192.168.72.46"
#define STR3 "192..72.46"
#define STR4 "192.72.46"
#define STR5 "192.168."
#define STR6 "192.168.72.46.23.44"
#define STR7 ".1923.168.72.46"
#define STR8 "192.168.#72.46"
unsigned long str_to_ip(char *str)
{
unsigned long ip = 0;unsigned long tmp = 0;char dotcount = 0, numcount = 0;
//First character cannot be anything ohter than
// '0' to '9'
if(*str > '9' || *str < '0')
return 0;
while( *str )
{
//subsequent characters cannot be anything other than
// '0' to '9' and '.'
if((*str > '9' || *str < '0') && (*str != '.')
/*&& (*str != '\0')*/){
//printf("returning from first if in while\n");
return 0;
}
else{
if(*str == '.'){
//following character cannot be anything other than
// '0' to '9'
if(*(str+1) > '9' || *(str+1) < '0')
return 0;
if(numcount > 3)
return 0;
dotcount++;numcount = 0;
str++;
ip |= tmp << ((4 - dotcount)*8);
//printf("tmp = %ld dot count = %d\n", tmp << 24, dotcount);
//printf("IP = %ld\n", ip);
tmp = 0;
}
else{
tmp = (tmp * 10) + (*str - '0');
//printf("tmp = %ld num count = %d\n", tmp, numcount);
str++;
numcount++;
}
}
if(dotcount > 3)
return 0;
}
if(dotcount < 3)
return 0;
ip |= tmp ;
//printf("tmp = %ld dot count = %d\n", tmp, dotcount);
//printf("IP = %ld\n", ip);
return ip;
}
int main()
{
int i;
unsigned long ip = 0;
printf("Testing function to convert IPv4 addr in string form to integer\n");
printf("Input : %s Output : %ld\n", STR1, ip = str_to_ip(STR1));
printf("Input : %s Output : %ld\n", STR2, ip = str_to_ip(STR2));
printf("Input : %s Output : %ld\n", STR3, ip = str_to_ip(STR3));
printf("Input : %s Output : %ld\n", STR4, ip = str_to_ip(STR4));
printf("Input : %s Output : %ld\n", STR5, ip = str_to_ip(STR5));
printf("Input : %s Output : %ld\n", STR6, ip = str_to_ip(STR6));
printf("Input : %s Output : %ld\n", STR7, ip = str_to_ip(STR7));
printf("Input : %s Output : %ld\n", STR8, ip = str_to_ip(STR8));
}
#include <stdio.h>
int main (int argc, char **argv)
{
unsigned int octet = 0,ip_addr = 0;
char *str_ip, str[256];
str_ip = str;
printf("Please provide IPV4 address in string format \n");
scanf("%s", str_ip);
printf("IP address: %s\n", str_ip);
while (*str_ip) {
octet = octet * 10 + (*str_ip - '0');
printf("str %c octet 0x%x\n", *str_ip, octet);
str_ip++;
if ((*str_ip == '.') || (*str_ip == 0)) {
printf("ip_addr 0x%x\n", ip_addr);
ip_addr = (ip_addr << 8) | octet;
printf("ip_addr 0x%x\n", ip_addr);
octet = 0x0;
if (*str_ip)
str_ip++;
}
}
printf("IP Address Hex: 0x%x, network order 0x%x\n", ip_addr, htonl(ip_addr));
}
Removed debug statements...
#include <stdio.h>
int main (int argc, char **argv)
{
unsigned int octet = 0,ip_addr = 0;
char *str_ip, str[256];
str_ip = str;
printf("Please provide IPV4 address in string format \n");
scanf("%s", str_ip);
printf("IP address: %s\n", str_ip);
while (*str_ip) {
octet = octet * 10 + (*str_ip - '0');
str_ip++;
if ((*str_ip == '.') || (*str_ip == 0)) {
ip_addr = (ip_addr << 8) | octet;
octet = 0x0;
if (*str_ip)
str_ip++;
}
}
printf("IP Address Hex: 0x%x, network order 0x%x\n", ip_addr, htonl(ip_addr));
}
unsigned int ip_str_to_int(char *str) {
- shivacherukuri February 14, 2012unsigned int ip=0;
while(*str) {
ip = (ip *10) + (*str -'0');
*str++;
if (*str && *str++ == '.') //skip period
*str++;
}
return ip;
}
assuming passing valid four octet ip address string..u can add validation part before calling this function.