-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceReplacement.cpp
More file actions
46 lines (42 loc) · 1023 Bytes
/
SpaceReplacement.cpp
File metadata and controls
46 lines (42 loc) · 1023 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
#include <string>
using namespace std;
class Solution {
public:
/*
* @param string: An array of Char
* @param length: The true length of the string
* @return: The true length of new string
*/
int replaceBlank(char string[], int length) {
// write your code here
int nSpaceNum = 0;
for (int i = 0; i < length; i++)
{
if (string[i] == ' ')
{
nSpaceNum++;
}
}
int left = length;
int right = length + 2 * nSpaceNum;
while (left != right)
{
if (string[left] == ' ')
{
string[right--] = '0';
string[right--] = '2';
string[right--] = '%';
left--;
}
else
{
string[right--] = string[left--];
}
}
return length + 2 * nSpaceNum;
}
};
int main(int argc, char const *argv[]) {
/* code */
return 0;
}