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 <stdio.h>
#include <assert.h>
#include <stdlib.h>
void
replace_space(
char
*str)
{
assert
(str);
char
*pstr = str;
int
space = 0;
int
len = 0;
int
newlen = 0;
while
(*str)
{
if
(*str ==
' '
)
space++;
len++;
str++;
}
newlen = len + space * 2;
char
*newpstr = pstr + newlen - 1;
char
*oldpstr = pstr + len - 1;
while
(oldpstr < newpstr)
{
if
(*oldpstr ==
' '
)
{
*newpstr-- =
'0'
;
*newpstr-- =
'2'
;
*newpstr-- =
'%'
;
}
else
{
*newpstr-- = *oldpstr;
}
oldpstr--;
}
}
int
main()
{
char
str[20] =
"we are happy."
;
replace_space(str);
printf
(
"%s\n"
, str);
system
(
"pause"
);
return
0;
}
|
本文转自 七十七快 51CTO博客,原文链接:http://blog.51cto.com/10324228/1686631