-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringSubstitution.c
68 lines (61 loc) · 1.09 KB
/
StringSubstitution.c
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
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
void CharactersToReplace(char str[])
{
int i = 0,j = 0;
while (1)
{
if ('\0' != str[i])
{
if (' ' == str[i])
{
str = (char *)realloc(str, 3 * sizeof(char)); //Re open storage space
for (j = sizeof(str)+1; j > i; j--) //Move three spaces after the space, in order to prepare for the replacement of the front
{
str[j + 3] = str[j];
}
str[i] = '%';
str[i + 1] = '2';
str[i + 2] = '0';
}
}
else
{
printf("\n");
break;
}
i++;
}
}
void PrintToReplace(char str[])
{
int i = 0;
while (1)
{
if ('\0' != str[i])
{
if (' ' == str[i])
printf("%%20");
else
printf("%c", str[i]);
}
else
{
printf("\n");
break;
}
i++;
}
}
int main()
{
char str[] = "We are happy.";
CharactersToReplace(str);
PrintToReplace(str);
return 0;
}
//Problem description
//String substitution Spaces :
//Please implement a function, every space in the character array to replace "% 20".
//For example input "we are happy.", then output "so.we % 20 are % 20 happy