-
Notifications
You must be signed in to change notification settings - Fork 643
/
1.5.cpp
71 lines (69 loc) · 1.27 KB
/
1.5.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
67
68
69
70
71
#include <iostream>
#include <cstring>
using namespace std;
char* replace1(char *c){
if(c == NULL) return NULL;
int len = strlen(c);
if(len == 0) return NULL;
int cnt = 0;
for(int i=0; i<len; ++i)
{
if(c[i] == ' ')
++cnt;
}
char *cc = new char[len+2*cnt+1];
int p = 0;
for(int i=0; i<len; ++i)
{
if(c[i] == ' ')
{
cc[p] = '%';
cc[p+1] = '2';
cc[p+2] = '0';
p += 3;
}
else
{
cc[p] = c[i];
++p;
}
}
cc[p] = '\0';
return cc;
}
void replace2(char *c){
if(c == NULL) return;
int len = strlen(c);
if(len == 0) return;
int cnt = 0;
for(int i=0; i<len; ++i)
{
if(c[i] == ' ')
++cnt;
}
int p = len + 2*cnt;
c[p--] = '\0';//the space must be allocated first.
for(int i=len-1; i>=0; --i)
{
if(c[i] == ' ')
{
c[p] = '0';
c[p-1] = '2';
c[p-2] = '%';
p -= 3;
}
else
{
c[p] = c[i];
--p;
}
}
}
int main(){
const int len = 100;
char c[len] = "";
cout<<replace1(c)<<endl;
replace2(c);
cout<<c<<endl;
return 0;
}