本篇是第4题_替换空格的C语言实现:
题目描述:请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
代码如下:
#include<stdio.h>
#include<string.h>
#define N 1000
void ReplaceBlank(char*,int);
int main(int argc, char* argv[])
{
//char []= "ffff"
char str[N];
freopen("test.txt","r",stdin);
int count = 0,i = 0;
while (scanf("%c",&str[i]) != EOF)
{
if(str[i] == ' ')
count ++;
else{}
i ++;
}
str[i] = '\0'; // 注意不要丢了这一句
ReplaceBlank(str,count);
printf("%s",str);
}
void ReplaceBlank(char str[],int count)
{
int num = strlen(str); //不包含最后的'/0'
char *pt1,*pt2;
pt1 = &str[num + 1];
pt2 = &str[num + 2*count + 1]; //有多少个空格,就会增加2*count个字节空间
while (pt1 != pt2)
{
if (*pt1 == ' ')
{
pt1--;
*pt2-- = '0';
*pt2-- = '2';
*pt2-- = '%';
}
else
{
*pt2-- = *pt1--;
}
}
}
测试样例和更详细的题目描述,可以参考我的github:Matthew-Haonan的github.
持续更新,欢迎留言讨论。