File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ /* Given a valid (IPv4) IP address, return a defanged version of that IP address.
2+ A defanged IP address replaces every period "." with "[.]".*/
3+
4+ /* Input: address = "1.1.1.1"
5+ Output: "1[.]1[.]1[.]1"*/
6+
7+ #include < bits/stdc++.h>
8+ class Solution {
9+ public:
10+ string defangIPaddr (string address) {
11+ string op;
12+ // A for loop which will execute till the address[i] which means the string runs till the last character of the string is Null
13+ for (int i=0 ;address[i]!=' \0 ' ;i++)
14+ {
15+ // if address[i]== '.' which means that if the in a string contains a period we'll replace it with [.]
16+ if (address[i]==' .' )
17+ {
18+ // This line adds the square brackets if there's a period in a string
19+ op=op+' [' + address[i]+' ]' ;
20+ i++;
21+
22+ }
23+ op=op+address[i];
24+
25+ }
26+ return op;
27+ }
28+ };
You can’t perform that action at this time.
0 commit comments