Input an IPv4 address. Create a C program that checks which class it belongs to. In addition, print comments for the network address, the host address, the broadcast address, and the default mask address.
C Program
// C program to determine Class, Network ID, Host ID, Broadcast Id and Default Mask ID of an IPv4 address#include<stdio.h>#include<string.h>// Function to find out the ClasscharfindClass(char str[]){// storing first octet in arr[] variablechar arr[4];int i =0;while(str[i]!='.'){
arr[i]= str[i];
i++;}
i--;// converting str[] variable into number for// comparisonint ip =0, j =1;while(i >=0){
ip = ip +(str[i]-'0')* j;
j = j *10;
i--;}// Class Aif(ip >=1&& ip <=126)return'A';// Class Belseif(ip >=128&& ip <=191)return'B';// Class Celseif(ip >=192&& ip <=223)return'C';// Class Delseif(ip >=224&& ip <=239)return'D';// Class Eelsereturn'E';}// Function to separate Network ID as well as Host ID, Broadcast Id and Default Mask ID and print themvoidseparate(char str[],char ipClass){// Initializing network and host array to NULLchar network[12], host[12];for(int k =0; k <12; k++)
network[k]= host[k]='\0';// for class A, only first octet is Network ID// and rest are Host IDif(ipClass =='A'){int i =0, j =0;while(str[j]!='.')
network[i++]= str[j++];
i =0;
j++;while(str[j]!='\0')
host[i++]= str[j++];printf("Network ID: %s\n", network);printf("Host ID: %s", host);printf("Default subnet mask: 255.0.0.0\n");printf("Broadcast ID:%s.255.255.255\n",network);}// for class B, first two octet are Network ID// and rest are Host IDelseif(ipClass =='B'){int i =0, j =0, dotCount =0;// storing in network[] up to 2nd dot// dotCount keeps track of number of// dots or octets passedwhile(dotCount <2){
network[i++]= str[j++];if(str[j]=='.')
dotCount++;}
i =0;
j++;while(str[j]!='\0')
host[i++]= str[j++];printf("Network ID is %s\n", network);printf("Host ID is %s", host);printf("Default subnet mask is 255.255.0.0\n");printf("Broadcast ID:%s.255.255\n",network);}// for class C, first three octet are Network ID// and rest are Host IDelseif(ipClass =='C'){int i =0, j =0, dotCount =0;// storing in network[] up to 3rd dot// dotCount keeps track of number of// dots or octets passedwhile(dotCount <3){
network[i++]= str[j++];if(str[j]=='.')
dotCount++;}
i =0;
j++;while(str[j]!='\0')
host[i++]= str[j++];printf("Network ID is %s\n", network);printf("Host ID is %s", host);printf("Default subnet mask is 255.255.255.0\n");printf("Broadcast ID:%s.255\n",network);}// Class D and E are not divided in Network and Host IDelseprintf("In this Class, IP address is not divided into Network and Host ID\n");}//main functionintmain(){char str[20];printf("Please enter the IPV4 address: ");fgets(str,20,stdin);char ipClass =findClass(str);printf("Given IP address belongs to Class %c\n", ipClass);separate(str, ipClass);return0;}
Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…