-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_blank_replace2.cpp
111 lines (95 loc) · 1.7 KB
/
string_blank_replace2.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
请实现一个函数, 把字符串中的每个空格替换成"%20". 例如, 输入"We are happy.", 则输出"We%20are%20happy."
解法二
*/
#include <iostream>
using namespace std;
void blank_replace(char* string)
{
if (string == nullptr)
return;
int stringLength = 0,
blankCount = 0,
newStringLength = 0;
int i = 0;
while (string[i] != '\0')
{
stringLength += 1;
if (string[i] == ' ')
{
blankCount += 1;
}
i += 1;
}
newStringLength = stringLength + blankCount * 2;
int indexOfOriginal = stringLength;
int indexOfNew = newStringLength;
while (indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
{
if (string[indexOfOriginal] != ' ')
{
string[indexOfNew --] = string[indexOfOriginal];
}
else
{
string[indexOfNew --] = '0';
string[indexOfNew --] = '2';
string[indexOfNew --] = '%';
}
--indexOfOriginal;
}
}
void test(char* string, char* result)
{
blank_replace(string);
int i = 0;
while (string[i] != '\0')
{
if (string[i] != result[i])
{
throw "assert failed";
}
i += 1;
}
}
void test1()
{
char string[10] = "";
char result[] = "";
test((char*)string, (char*)result);
}
void test2()
{
char string[30] = "We are happy.";
char result[] = "We%20are%20happy.";
test((char*)string, (char*)result);
}
void test3()
{
char string[10] = " ";
char result[] = "%20";
test((char*)string, (char*)result);
}
void test4()
{
char string[10] = "123 ";
char result[] = "123%20";
test((char*)string, (char*)result);
}
void test5()
{
char string[20] = " 123 ";
char result[] = "%20123%20";
test((char*)string, (char*)result);
}
#if 1
int main(int argc, const char* argv[])
{
test1();
test2();
test3();
test4();
test5();
return 0;
}
#endif