Skip to content

Commit 9441d1f

Browse files
committed
Minor changes
1 parent 0926dcf commit 9441d1f

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

Strings/Validate_IP_address.cpp

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,54 @@ The function takes a string s as its only argument .
1919
2020
**************************************************************************/
2121

22+
// SOLUTION (in C++):
2223

23-
SOLUTION (in C++):
24+
#include <bits/stdc++.h>
2425

25-
#include<bits/stdc++.h>
26-
27-
using namesapce std;
26+
using namespace std;
2827

2928
int isValid(string s)
3029
{
31-
s += '.';
32-
int countDots = 0;
33-
string str = "";
34-
for(int i = 0;i<s.length();i++)
35-
{
36-
if(s[i] != '.')
37-
{
38-
if(s[i] >= 48 && s[i] <= 57)
39-
str += s[i];
40-
else
41-
return 0;
42-
}
43-
if(s[i] == '.')
44-
{
45-
if(str[0] == '.' && str.length() > 1)
46-
return 0;
47-
stringstream obj(str);
48-
int x = 0;
49-
obj>>x;
50-
if(x<0 || x>255)
51-
return 0;
52-
if(str.size() == 0)
53-
return 0;
54-
countDots++;
55-
str = "";
56-
}
57-
if(countDots == 4)
58-
return 1;
59-
else
60-
return 0;
30+
s += '.';
31+
int countDots = 0;
32+
string str = "";
33+
for (int i = 0; i < s.length(); i++)
34+
{
35+
if (s[i] != '.')
36+
{
37+
if (s[i] >= 48 && s[i] <= 57)
38+
str += s[i];
39+
else
40+
return 0;
41+
}
42+
if (s[i] == '.')
43+
{
44+
if (str[0] == '.' && str.length() > 1)
45+
return 0;
46+
stringstream obj(str);
47+
int x = 0;
48+
obj >> x;
49+
if (x < 0 || x > 255)
50+
return 0;
51+
if (str.size() == 0)
52+
return 0;
53+
countDots++;
54+
str = "";
55+
}
56+
if (countDots == 4)
57+
return 1;
58+
else
59+
return 0;
60+
}
6161
}
62-
63-
64-
int main()
65-
{
66-
string s;
67-
cin>>s;
68-
int k = isValid(s);
69-
if(k == 0)
70-
cout<<"Valid"<<endl;
71-
else
72-
cout<<"Not Valid"<<endl;
73-
return 0;
74-
}
62+
int main()
63+
{
64+
string s;
65+
cin >> s;
66+
int k = isValid(s);
67+
if (k == 0)
68+
cout << "Valid" << endl;
69+
else
70+
cout << "Not Valid" << endl;
71+
return 0;
72+
}

0 commit comments

Comments
 (0)