-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
49 lines (38 loc) · 705 Bytes
/
Copy pathmain.cpp
File metadata and controls
49 lines (38 loc) · 705 Bytes
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
// 将字符串的所有空格替换为%20
#include <iostream>
#include <algorithm>
#include <string>
std::string Replace(const std::string &str)
{
int spaceNum = std::count(str.begin(), str.end(), ' ');
if (spaceNum == 0)
return str;
int size = str.length() + spaceNum * 2;
if (size == 0)
return "";
std::string newStr(size + 1, 0);
int j = size - 1;
for (int i = str.length() - 1; i >= 0; --i)
{
if (str[i] == ' ')
{
newStr[j--] = '0';
newStr[j--] = '2';
newStr[j--] = '%';
}
else
newStr[j--] = str[i];
}
return newStr;
}
int main()
{
std::string str;
char ch = 0;
while ((ch = getchar()) != '\n')
{
str.push_back(ch);
}
std::cout << Replace(str);
return 0;
}