0% found this document useful (0 votes)
48 views4 pages

Computer Networks

The document describes a C program that takes an IP address as input and determines its class. It extracts the individual octets of the IP into an array, then checks the first octet against number ranges to classify it as Class A, B, C, D or E and print the result. The program extracts the IP address from a string into component octets, then analyzes the first octet to determine the class and output the class. It was tested and successfully classified sample IP addresses.

Uploaded by

Aeydap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

Computer Networks

The document describes a C program that takes an IP address as input and determines its class. It extracts the individual octets of the IP into an array, then checks the first octet against number ranges to classify it as Class A, B, C, D or E and print the result. The program extracts the IP address from a string into component octets, then analyzes the first octet to determine the class and output the class. It was tested and successfully classified sample IP addresses.

Uploaded by

Aeydap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ROLL NO.

: 18CSR028

CN LAB /2020-21/ODD/A SECTION


ROLL NO.: 18CSR028
Ex.No. 7 WRITE A SIMPLE PROGRAM
:
Date : 07.12.20 TO FIND THE CLASSES OF AN IP ADDRESS
20

Ex.No. :

Date :

WRITE A SIMPLE PROGRAM

TO FIND THE CLASSES OF AN IP ADDRESS

Aim :

To find the classes of an IP address.

Coding :

#include <stdio.h>

#include <string.h>

void extractIpAddress(unsigned char *sourceString,short *ipAddress)

unsigned short len=0;

unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5];

len=strlen(sourceString);

for(i=0;i<len;i++)

if(sourceString[i]!='.'){

buf[cnt++] =sourceString[i];

if(sourceString[i]=='.' || i==len-1){

buf[cnt]='\0';

cnt=0;

CN LAB /2020-21/ODD/A SECTION


ROLL NO.: 18CSR028
oct[cnt1++]=atoi(buf);

ipAddress[0]=oct[0];

ipAddress[1]=oct[1];

ROLL NO.: 18CSR028ipAddress[2]=oct[2];

ROLL NO.: 18CSR028

ipAddress[3]=oct[3];

int main()

unsigned char ip[20]={0};

short ipAddress[4];

printf("\nEnter the IP Address : ");

scanf("%s",ip);

extractIpAddress(ip,&ipAddress[0]);

printf("\nIp Address: %03d. %03d.


%03d\n\n\n",ipAddress[0],ipAddress[1],ipAddress[2],ipAddress[3]);

if(ipAddress[0]>=0 && ipAddress[0]<=127)

printf("Given Ip Address of Class A.\n");

if(ipAddress[0]>127 && ipAddress[0]<191)

printf("Given Ip Address of Class B.\n");

if(ipAddress[0]>191 && ipAddress[0]<224)

printf("Given Ip Address of Class C.\n");

if(ipAddress[0]>224 && ipAddress[0]<=239)

printf("Given Ip Address of Class D.\n");

if(ipAddress[0]>239)

CN LAB /2020-21/ODD/A SECTION


ROLL NO.: 18CSR028
printf("Given Ip Address of Class E.\n");

return 0;

}ROLL NO.: 18CSR028

Sample I/O:

ScreenshotROLL NO.: 18CSR028

Result:

The program to find the classes of an IP address has been successfully

Verified.

CN LAB /2020-21/ODD/A SECTION

You might also like