1108. IP 地址无效化
为保证权益,题目请参考 1108. IP 地址无效化(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class Solution
{
public:
string defangIPaddr(string address)
{
int offset = 0;
int pos;
while ((pos = address.find(".", offset)) != address.npos)
{
address.replace(pos, 1, "[.]");
offset = pos + 2;
}
return address;
}
};
int main()
{
Solution so;
string res("127.0.0.1");
cout << so.defangIPaddr(res);
return 0;
}
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
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