-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetidhostidnetclass.cpp
108 lines (106 loc) · 2.5 KB
/
netidhostidnetclass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
#include<string.h>
using namespace std ;
// Function to find out the class
char findClass(char str[])
{
//storing first octet in arr[] variable
char arr[4] ;
int i=0 ;
while(str[i]!='.'){
arr[i] = str[i] ;
i++ ;
}
i-- ;
//converting str[] variable into number for comparison
int ip=0,j=1 ;
while(i>=0){
ip = ip + (str[i]-'0')*j ;
j = j*10 ;
i-- ;
}
//class A
if(ip>=1 && ip<=126)
return 'A' ;
//class B
else if(ip>=127 && ip<=191)
return 'B' ;
//class C
else if(ip>=192 && ip<=223)
return 'C' ;
//class D
else if(ip>=224 && ip<=239)
return 'D' ;
//class E
else
return 'E' ;
}
// Function to separate Network id as well as Host id and print them
void separate(char str[],char ipClass)
{
//initializing network and host array to NULL
char 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 id
if(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++] ;
cout<<"Network ID : "<<network<<endl ;
cout<<"Host ID : "<<host<<endl ;
}
//for class B, first two octets are Network id and rest are Host id
else if(ipClass=='B')
{
int i=0,j=0,dotCount=0 ;
//storing in network[] upto two dots
//dotCount keeps track of number of octets passed
while(dotCount<2){
network[i++] = str[j++] ;
if(str[j]=='.')
dotCount++ ;
}
i=0 ;
j++ ;
while(str[j]!='\0')
host[i++] = str[j++] ;
cout<<"Network ID : "<<network<<endl ;
cout<<"Host ID : "<<host<<endl ;
}
//for class C, first three octets are Network id and rest are Host id
else if(ipClass=='C')
{
int i=0,j=0,dotCount=0 ;
//storing in network[] upto three dots
//dotCount keeps track of number of octets passed
while(dotCount<3){
network[i++] = str[j++] ;
if(str[j]=='.')
dotCount++ ;
}
i=0 ;
j++ ;
while(str[j]!='\0')
host[i++] = str[j++] ;
cout<<"Network ID : "<<network<<endl ;
cout<<"Host ID : "<<host<<endl ;
}
//classes D and E are not divided into Network and Host id
else
cout<<"In this class, IP address is not divided into Network and Host id..\n" ;
}
// Driver Function to test the above function
int main()
{
char str[] = "192.226.12.11" ;
char ipClass = findClass(str) ;
cout<<"Given IP address belongs to class "<<ipClass<<endl ;
separate(str,ipClass) ;
return 0 ;
}