forked from Astha369/CPP_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidate_IP-Address.cpp
65 lines (55 loc) · 1.33 KB
/
Validate_IP-Address.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
/*
Example 1:
Input:
IPv4 address = 222.111.111.111
Output: 1
Explanation: Here, the IPv4 address is as
per the criteria mentioned and also all
four decimal numbers lies in the mentioned
range.
Example 2:
Input:
IPv4 address = 5555..555
Output: 0
Explanation: 5555..555 is not a valid
IPv4 address, as the middle two portions
are missing.
*/
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
class Solution {
public:
int isValid(string s) {
int count = 0;
string result = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] != '.') {
result.push_back(s[i]);
if (!isdigit(s[i])) {
return false;
}
} else {
if ((result.length() > 1 && result[0] == '0') || result.empty() || stoi(result) < 0 || stoi(result) > 255) {
return false;
}
count++;
result = "";
}
}
if (count != 3 || (result.length() > 1 && result[0] == '0') || result.empty() || stoi(result) < 0 || stoi(result) > 255) {
return false;
}
return true;
}
};
int main(){
int t;
cin>>t;
while(t--){
string s;
cin>>s;
Solution ob;
cout<<ob.isValid(s)<<endl;
}
}