-
Notifications
You must be signed in to change notification settings - Fork 6
/
04_ReplaceBlank.cpp
66 lines (55 loc) · 981 Bytes
/
04_ReplaceBlank.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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void replace_black(char strs[], int length)
{
int i, j, count = 0, original_length = 0;
if (strs == NULL || length <= 0)
return ;
for (i = 0; strs[i] != '\0'; i ++)
{
original_length ++;
if (strs[i] == ' ')
count ++;
}
//cout << length << endl;
int newlength = count * 2 + original_length;
if (newlength > length)
return ;
i = original_length - 1;
j = newlength - 1;
strs[newlength] = '\0';
while (i <= j && i >= 0)
{
if (strs[i] == ' ')
{
strs[j --] = '0';
strs[j --] = '2';
strs[j --] = '%';
}
else
strs[j --] = strs[i];
i --;
}
cout << strs << endl;
return;
}
int main(void)
{
const size_t array_size = 1000;
char strs[array_size];
//gets_s(strs, array_size - 1);
int i = 0;
char tmp;
tmp = cin.get();
while(tmp != '\n')
{
strs[i] = tmp;
tmp = cin.get();
i ++;
}
strs[i] = '\0';
replace_black(strs, array_size);
return 0;
}