-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
48 lines (35 loc) · 1002 Bytes
/
main.cpp
File metadata and controls
48 lines (35 loc) · 1002 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
// main.cpp
#include <iostream>
#include <map>
#include <string>
std::string& urlify(std::string& str);
int main() {
std::string myString = "Hello, world!";
std::cout << urlify(myString) << std::endl;
return 0;
}
std::string& urlify(std::string& str) {
int counter = 0;
for (char c: str) {
if (c == ' ') {
counter++;
}
}
int oldSize = str.size();
int newSize = oldSize + counter * 2;
str.resize(newSize);
int oldStringCounter = oldSize - 1;
int newStringCounter = newSize - 1;
for (; oldStringCounter > 0; --oldStringCounter) {
if (str[oldStringCounter] == ' ') {
str[newStringCounter] = '0';
str[newStringCounter - 1] = '2';
str[newStringCounter - 2] = '%';
newStringCounter -= 2;
} else {
str[newStringCounter] = str[oldStringCounter];
}
newStringCounter--;
}
return str;
}