替换空格

题目描述:

请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

输入:

每个输入文件仅包含一组测试样例。
对于每组测试案例,输入一行代表要处理的字符串。

输出:

对应每个测试案例,出经过处理后的字符串。

样例输入:
We Are Happy
样例输出:
We%20Are%20Happy
 1 #include<stdio.h>
 2 #include<string.h>
 3 void replaceSpace(char ch[])
 4 {
 5     int i = 0;
 6     int conlength= 0;
 7     int spaceNum = 0;
 8     int newLength= 0;
 9     char *p1,*p2;
10     while(ch[i] !='')
11     {
12         if(ch[i]==' ')
13             spaceNum++;
14         i++;
15     }
16     newLength = i + 2*spaceNum;
17     p1 = &ch[i];
18     p2 = &ch[newLength];
19     while (p1 != p2)
20     {
21         if (*p1 != ' ')
22         {
23             *p2 = *p1;
24         }
25         else
26         {
27             p2-=2;
28             strncpy(p2,"%20",3);
29         }
30         p1--;
31         p2--;
32     }
33 }
34 void main()
35 {
36     char ch[1000001]={''};
37         while(gets(ch))
38 {
39     replaceSpace(ch);
40     puts(ch);    
41 }
42 }
43 /**************************************************************
44     Problem: 1510
45     User: xuebintian
46     Language: C
47     Result: Accepted
48     Time:50 ms
49     Memory:1816 kb
50 ****************************************************************/
原文地址:https://www.cnblogs.com/churi/p/3734684.html