Placement Papers - Cisco

Cisco Systems
CISCO - 27Apr2012
Posted by :
David Rajesh
(35)
Hi all, I attended Cisco interview for STB Development in C, RTOS, DVB.

They asked the below questions as,

Get an IP address in char * and store it in uint32 variable and display with \'.\' sysmbol. I worked out and find the below program for your knowledge.

/* Convert IP address Char array to integer array using strtok */

#include<stdio.h>
int main()
{
 char acIPAddr[ 16 + 1 ] = "121.122.123.124";
 unsigned int aui32IPAddr[ 4 ];

 unsigned int i = 0;

 aui32IPAddr[0] = atoi( strtok( acIPAddr, "." ) );

 for(i=1; i!=4; i++)
 {
  aui32IPAddr[ i ] = atoi( strtok(NULL, ".") );
 }

 printf("IPAddr[%d.%d.%d.%d]\n", aui32IPAddr[0], aui32IPAddr[1], aui32IPAddr[2], aui32IPAddr[3] );

 return 0;
}


/* Convert IP address char * to uint32 using sscanf */

#include<stdio.h>
int main()
{
  char * pacIPAddress;
  unsigned int aui8IPAddr[4], i;
  unsigned long int ui32IPAddress;

  pacIPAddress = ( char *) malloc ( sizeof(char) * 16 + 1 );

  pacIPAddress = "121.122.123.124";
  
  sscanf ( pacIPAddress, "%d.%d.%d.%d", &aui8IPAddr[0], &aui8IPAddr[1], &aui8IPAddr[2], &aui8IPAddr[3] );

  ui32IPAddress = ( ( aui8IPAddr[0] << 24 ) | ( aui8IPAddr[1] << 16 ) | ( aui8IPAddr[2] << 8 ) | aui8IPAddr[3] );

  printf ( "ui32IPAddress[%d]\n", ui32IPAddress );

  for ( i=0; i<4; i++ )
  {
    aui8IPAddr[i] = ( ui32IPAddress >> (i*8) ) & 0xFF;
  }

  printf("IP Addr in Interger[%d].[%d].[%d].[%d]\n", aui8IPAddr[3], aui8IPAddr[2], aui8IPAddr[1], aui8IPAddr[0] );

  return 0;
}