-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode_1108.cpp
53 lines (46 loc) · 1.12 KB
/
LeetCode_1108.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
/*************************************************************************
> File Name: LeetCode_1108.cpp
> Author:
> Mail:
> Created Time: Sat 22 Feb 2020 05:45:59 PM PST
************************************************************************/
#include<iostream>
#include<string>
using namespace std;
string DefangedIPAddr(string& address)
{
for (int i = 0; i < address.length(); i++)
if (address[i] == '.')
{
address.replace(i, 1, "[.]", 3);
i += 3;
}
return address;
}
void test(const char* testName, string& address, string& expected)
{
if (testName == nullptr)
return;
printf("%s:", testName);
if (DefangedIPAddr(address) == expected)
printf("Passed.\n");
else
printf("%s.Failed.\n", DefangedIPAddr(address).c_str());
}
void test1()
{
string address = "1.1.1.1";
string expected = "1[.]1[.]1[.]1";
test("Test1", address, expected);
}
void test2()
{
string address = "255.100.50.0";
string expected = "255[.]100[.]50[.]0";
test("Test2", address, expected);
}
int main()
{
test1();
test2();
}