Samsung Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
#include <stdio.h>
#include <conio.h>
int main ()
{
int a[6]; int ab,i=0;
char mac[]="aa:bb:cc:12:23:34";
char *aa=NULL;
char delims[] = ":";
getch();
ab= (int)strtok(mac,delims);
while (ab!=NULL)
{
printf(" %d %s ",ab,(char *)ab);
a[i]=ab;
i++;
ab= (int) strtok(NULL,delims);
}
getch();
}
This Question is very much Simple,By Asking Such Question Interviewer wanted to check your Approach from Basic to Advance,In My Opinion,We should Give approach ,Idea rather than Direct Code or Answer.
1) Check Whether Given MAC Address is in Correct Format or not
ab:cd:ef:12:34:56
As per the IEEE Standard Above mentioned is correct
2)At locations 2,5,8,11,14 Check for the COLON Character,If it is not present then Show an error like "Invalid MAC address"
3) Every Value
Should be in the pair of two.
ab:cd:ef:2:4:56 <--Incorrect
ab:cd:ef:02:04:56<--Correct
4) Hex Values must be in the range of a to f,Otherwise Display an error like "Inavlid MAC address"
5) Now Tell him about Character Pair value to int Hex Value As asked in the Question.
I know my idea was so Basic but it may help to some Beginner @ Careercup
int toInteger(char ch)
{
if(ch>='0'&&ch<='9')
{
return ch-'0';
}
else
return
ch-'a'+10;
}
int main()
{
int mac[6]={0},index,i;
i=index=0;
char *string="ab:cd:ef:12:34:56";
while(*string)
{
if(*string!=':')
{
mac[index]=mac[index]*16+toInteger(*string);
}
else
{
index++;
}
string++;
}
for(i=0;i<6;i++)
{
printf("%d ",mac[i]);
}
return 0;}
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int i, j;
char *mac_add = "ab:cd:ef:12:34:56";
char result[strlen(mac_add)];
j = 0;
for(i = 0; i <= strlen(mac_add); i++)
{
if(mac_add[i] != ':')
{
result[j++] = mac_add[i];
}
else
{
result[j++] = ',';
result[j++] = ' ';// Not required
// j++;
}
}
puts(result);
return 0;
}
typedef struct bin{
unsigned char a1:4;
unsigned char a2:4;
} hex;
#pragma pack(1)
typedef struct {
hex d[6];
} max;
void Macasctohex()
{
char mac[]="12:34:56:ab:cd:ef";
max opt;
int j=0;
memset(&opt,0,sizeof(opt));
for(int i=0;i<sizeof(mac)/sizeof(mac[0]);i+=3)
{
opt.d[j].a1=((mac[i+1]>='a')?(mac[i+1]-'a'+10):(mac[i+1]-'0'));
opt.d[j++].a2=((mac[i]>='a')?(mac[i]-'a'+10):(mac[i]-'0'));
}
}
If we know that the MAC address is well-formed (and should not handle error cases) then this will do
And it is demonstrating that you know a bit more about the C I/O library than others.
- Selmeczy, Péter December 14, 2011