Given a valid IPv4 address in the form of string and it follows Class Full addressing. The task is to determine the class of the given IPv4 address as well as separate the Network and Host ID parts from it.
Examples:
Input : 1.4.5.5
Output :
Given IP address belongs to Class A
Network ID is 1
Host ID is 4.5.5
Input : 130.45.151.154
Output :
Given IP address belongs to Class B
Network ID is 130.45
Host ID is 151.154
Approach
- For determining the class: The idea is to check the first octet of the IP addresses. As we know, for class A first octet will range from 1 - 126, for class B first octet will range from 128 - 191, for class C first octet will range from 192- 223, for class D first octet will range from 224 - 239, for class E first octet will range from 240 - 255.
- For determining the Network and Host ID: We know that Subnet Mask for Class A is 8, for Class B is 16 and for Class C is 24 whereas Class D and E are not divided into Network and Host ID.
For 2nd Example, the first octet is 130. So, it belongs to Class B. Class B has a subnet mask of 16. So, the first 16 bit or first two octets are the Network ID part and the rest is the Host ID part.
Hence, the Network ID is 130.45 and the Host ID is 151.154
Output:
Given IP address belongs to Class C
Network ID is 192.226.12
Host ID is 11