-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidateIPAddress.cpp
60 lines (54 loc) · 2.13 KB
/
ValidateIPAddress.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
//CODING NINJAS
// Program to validate IP address
// https://www.codingninjas.com/studio/problems/program-to-validate-ip-address_981315
// You are given the text ‘IPAddress’. Your task is to check if the given text ‘IPAddress’ is a valid ‘IPv4’ or not.
// Sample Input 1:
// 4
// 123.111.12.k
// 122.0.330.0
// 1.1.1.250
// 1.0.0.0.1
// Sample Output 1:
// False
// False
// True
// False
// Explanation Of Sample Input 1:
// Test Case 1:
// Given text ‘IPAddress = 123.111.12.k’, it is satisfying the first condition that given ‘IPAddress’ must be ‘a.b.c.d’ formed but it not satisfying the second condition that d must in a range of ‘0’ to ‘255’ but the value of ‘d’ is ‘k’.
// Hence return ‘False’.
// Test Case 2:
// Given text ‘IPAddress = 122.0.330.0’, it is satisfying the first condition that given ‘IPAddress’ must be ‘a.b.c.d’ formed but it not satisfying the second condition that c must in a range of ‘0’ to ‘255’ but the value of ‘c’ is ‘330’ and it is out of range.
// Hence return ‘False’.
// Test Case 3:
// Given text ‘IPAddress = 1.1.1.250’, it is satisfying the first condition that given ‘IPAddress’ must be ‘a.b.c.d’ formed as well as it satisfying the second condition that a,b,c, and d must in range of ‘0’ to ‘250’.
// Hence return ‘True’.
// Test Case 4:
// Given text ‘IPAddress = 1.0.0.0.1’, it is not satisfying the first condition for valid ‘IPv4’, that text ‘IPAddress’ must be in form of ‘a.b.c.d’ but given text is a form of ‘a.b.c.d.e’
// Hence return ‘False’.
#include <bits/stdc++.h>
bool isValidIPv4(string ipAddress) {
stringstream s(ipAddress);
string word;
int count = 0;
while(!s.eof()){
count++;
if(count > 4){
return false;
}
getline(s, word, '.');
if(isalpha(word[0])){
return false;
}
else if(isdigit(word[0])){
int val = stoi(word);
if(val > 255 || val < 0){
return false;
}
}
}
if(count < 4){
return false;
}
return true;
}