-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipAddress.cpp
101 lines (72 loc) · 2.15 KB
/
ipAddress.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
#include<bits/stdc++.h>
using namespace std;
bool valid_subpart(string s, int i, int j){
int len = j-i+1;
if(len >3)
return false;
if(s[i] == '0')
{
if(len == 1) return true;
return false;
}
int num = stoi(s.substr(i,j-i+1));
if(num>=0 && num<=255)
return true;
return false;
}
bool valid_ip(string s, int len, int i, int j, int k)
{
if( valid_subpart(s,0,i) &&
valid_subpart(s,i+1,j) &&
valid_subpart(s,j+1,k) &&
valid_subpart(s,k+1,len-1 ) )
return true;
return false;
}
void add_string(string s, int n, int i, int j, int k,vector<string> &res){
string temp;
temp.append(s.substr(0,i+1));
temp.push_back('.');
temp.append(s.substr(i+1,j-i));
temp.push_back('.');
temp.append(s.substr(j+1,k-j));
temp.push_back('.');
temp.append(s.substr(k+1,n-k-1));
res.push_back(temp);
}
vector<string> genIp(string &s) {
int i,j,k,n;
vector<string> res;
n = s.length();
for(i=0;i<n-3;i++){
for(j=i+1;j<n-2;j++){
for(k=j+1;k<n-1;k++){
if(valid_ip(s,n,i,j,k)){
add_string(s,n,i,j,k,res);
}
}
}
}
return res;
}
int main()
{
cout<<endl<<"==== IP ADDRESS GENERATOR ===="<<endl;
string ipString;
cout<<endl<<"Inputs Section : ";
cout<<endl<<"Enter the numbers with which you wanna generate IP Address : ";
cin>>ipString;
vector<string> ans = genIp(ipString);
if (ans.size() == 0){
cout<<endl<<"Error : IP Address with the numbers given "<<ipString<<" not Possible -_-"<<endl;
}
else{
cout<<"Your Problem solved..........."<<endl;
cout<<"The IP address found are : "<<endl<<endl;
cout<<endl<<"==========================================="<<endl<<endl<<endl;
for (int i = 0;i<ans.size();i++){
cout<<" | "<<"IP ADDDRESS "<<i+1<<" : "<<ans[i]<<" | "<<endl<<endl;
}
cout<<endl<<"==========================================="<<endl;
}
}