-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_space.c
More file actions
50 lines (37 loc) · 932 Bytes
/
replace_space.c
File metadata and controls
50 lines (37 loc) · 932 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
49
/*
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
*/
#include <stdio.h>
#include <stdlib.h>
void replace_space(char *str, int length)
{
int space_count = 0;
for (int i = 0; i < length; i ++)
{
if (str[i] == ' ') {
space_count ++;
}
}
int ptr = length + space_count * 2 - 1;
for (int i = length - 1; i >= 0; i --)
{
if (str[i] != ' ') {
str[ptr--] = str[i];
}
else {
str[ptr--] = '0';
str[ptr--] = '2';
str[ptr--] = '%';
}
}
str[length + space_count * 2] = '\0';
}
int main()
{
char *str = malloc(1024);
snprintf(str, 1024, "%s", "We are Happy");
replace_space(str, 12);
printf("result: %s\n", str);
free(str);
return 0;
}