标签:空格的替换
字符串替换空格:请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“we are happy.”,则输出“we%20are%20happy.”。
#include <stdio.h>
#include <assert.h>
void replace_black(char *str)
{
assert(str);
int black = 0;
int oldlen = strlen(str);
int newlen = 0;
char *tmp = str;
while (*tmp)
{
if (*tmp == ‘ ‘)
black++;
tmp++;
}
newlen = oldlen + 2 * black;
while (oldlen < newlen)
{
if (str[oldlen] != ‘ ‘)
{
str[newlen--] = str[oldlen--];
}
else
{
str[newlen--] = ‘0‘;
str[newlen--] = ‘2‘;
str[newlen--] = ‘%‘;
oldlen--;
}
}
}
int main()
{
char p[20] = "we are happy";
replace_black(p);
printf("%s\n", p);
system("pause");
return 0;
}
本文出自 “1” 博客,请务必保留此出处http://10808695.blog.51cto.com/10798695/1715570
标签:空格的替换
原文地址:http://10808695.blog.51cto.com/10798695/1715570