Skip to content

Commit 72228e1

Browse files
authored
Uploading First Time (#45)
* Add files via upload * Update Defanging an IP Address.cpp
1 parent a71337b commit 72228e1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

C++/Defanging an IP Address.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
};

0 commit comments

Comments
 (0)