 # DCODIP - Editorial

Tester: Priyam

EASY-MEDIUM

## PROBLEM:

Decode the given string S and find an IP address (IPv4) of Class B (128 - 191) after sorting the given string S, and converting the first element of each octet in binary.

## QUICK EXPLANATION:

First copy the input string in a new variable without dot (.) and sort the new string using any sorting technique (e.g. Insertion Sort). Take the character of Index 0, 8, 17, 26 and find the ASCII value of these characters. These values will belong to class A IP address therefore we will add 63 in each for making a class B IP address. Now, convert each value in binary and print them separated by dot.

## EXPLANATION:

Before writing the code first solve the problem means think about the algorithm.
This is static type of problem here only one string S is given and we have convert it into the class B IP address that is in the binary form.
We just take the input in the list in form of single character (In C use the character array of size 35) and we know the position of every dot just remove the dot (it is dotted notation) and after that sort the string (use any sorting algorithm e.g. Insertion sort).
Take the sorted string by taking the first character of each octet. Take the 0, 8,17, 26 index add the 63 in the ASCII value of characters (because class B start from 128 )
and convert into binary form and put the dot(.) at index 9, 18, 27 index and print the result.

## SOLUTIONS:

Setter's Solution
``````#include <stdio.h>

int main()
{
char arr, temp;
int i, j = 0, key;
//Input the string
scanf("%s", arr);

//Removing . from string
for(i = 0; i < 35; i++)
if(arr[i] != '.')
temp[j++] = arr[i];

//Sorting the string using Insertion sort
for (i = 1; i <32; i++) {
key = temp[i];
j = i - 1;

while (j >= 0 && temp[j] > key) {
temp[j + 1] = temp[j];
j -= 1;
}
temp[j + 1] = key;
}

//Taking the first element of each octet
int num = {temp, temp, temp, temp};

//Coverting IP in Class B and printing binary
for(int k = 0; k < 4; k++) {
for (i = 0; i < 8; i++)
printf ("%d", (num[k]+63 << i & 1 << 7 ) ? 1 : 0);
if(k != 3)
printf(".");
}

return 0;
}

``````
Tester's Solution
``````
n = input()
k = list()
index = 8
for i in n:
k.append(i)
while (k.count('.')):
k.remove('.')
k.sort()
k.insert(index,  '.')
index = index + 9
for i in k:

for i in range(0,2):
result1 = ''
result2 = ''
result3 = ''
result4 = ''
while (w != 0):
remainder = w % 2
w = w // 2
result1 = str(remainder) + result1
while (x != 0):
remainder = x % 2
x = x// 2
result2 = str(remainder) + result2
while (y != 0):
remainder = y % 2
y = y// 2
result3 = str(remainder) + result3
while (z != 0):
remainder = z % 2
z = z//2
result4 = str(remainder) + result4
print(result1 + '.' + result2 + '.' + result3 + '.' + result4)

``````
1 Like