替换空格

替换空格

题目:请实现一个函数,把字符串中的每个空格替换成”%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。

思路: 首先准备两个指针:p1和p2。p1指向原始字符串的末尾,p2指向替换之后的字符串的末尾。接下来我们向前移动p1,逐个把它指向的字符串复制到p2指向的位置,知道遇到第一个空格为止。此时p1向前移动一格,在p2之前插入字符串”%20”,同时p2向前移动三格。

I a
p1(indexofOriginal) p2(indexofNew)
I a
p1 p2
I % 2 0 a
p1,p2
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
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
void ReplaceBlank(char str[], int length)
{
if(str == nullptr || length <= 0)
return ;
int OriginalLength = 0;
int numberofBlank = 0;
int i = 0;
while(str[i] != '\0')
{
++OriginalLength;
if(str[i] == ' ')
++numberofBlank;
++i;
}
int newLength = OriginalLength + numberofBlank * 2;
if(newLength > length)
return ;
int indexofOriginal = OriginalLength; //p1
int indexofNew = newLength; //p2
while(indexofOriginal >= 0 && indexofNew > indexofOriginal)
{
if(str[indexofOriginal] == ' ')
{
str[indexofNew--] = '0';
str[indexofNew--] = '2';
str[indexofNew--] = '%';
}
else
str[indexofNew--] = str[indexofOriginal];
--indexofOriginal;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ====================测试代码====================
void Test(char* testname, char str[], int length, char expected[])
{
if(testname != nullptr)
printf("%s begins: ", testname);
ReplaceBlank(str, length);
if(expected == nullptr && str == nullptr)
printf("Passed. \n");
else if(expected == nullptr && str != nullptr)
printf("Failed. \n");
else if(strcmp(str, expected) == 0)
printf("Passed. \n");
else
printf("Failed. \n");
}
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
//空格在句子中间
void Test1()
{
const int length = 50;
char str[length] = "hello world";
Test("test1", str, length, "hello%20world");
}
// 空格在句子开头
void Test2()
{
const int length = 50;
char str[length] = " helloworld";
Test("Test2", str, length, "%20helloworld");
}
// 空格在句子末尾
void Test3()
{
const int length = 50;
char str[length] = "helloworld ";
Test("Test3", str, length, "helloworld%20");
}
// 连续有两个空格
void Test4()
{
const int length = 50;
char str[length] = "hello world";
Test("Test4", str, length, "hello%20%20world");
}
// 传入nullptr
void Test5()
{
Test("Test5", nullptr, 0, nullptr);
}
// 传入内容为空的字符串
void Test6()
{
const int length = 50;
char str[length] = "";
Test("Test6", str, length, "");
}
//传入内容为一个空格的字符串
void Test7()
{
const int length = 50;
char str[length] = " ";
Test("Test7", str, length, "%20");
}
// 传入的字符串没有空格
void Test8()
{
const int length = 50;
char str[length] = "helloworld";
Test("Test8", str, length, "helloworld");
}
// 传入的字符串全是空格
void Test9()
{
const int length = 50;
char str[length] = " ";
Test("Test9", str, length, "%20%20%20");
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
return 0;
}
-------------本文结束感谢您的阅读-------------
很有帮助,打赏感谢!
0%