#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void replace_space(char *str)
{
assert(str);
char *pstr = str;
int space = 0;
int len = 0;
int newlen = 0;
while (*str)
{
if (*str == ‘ ‘)
space++;
len++;
str++;
}
newlen = len + space * 2;
char *newpstr = pstr + newlen - 1;
char *oldpstr = pstr + len - 1;
while (oldpstr < newpstr)
{
if (*oldpstr == ‘ ‘)
{
*newpstr-- = ‘0‘;
*newpstr-- = ‘2‘;
*newpstr-- = ‘%‘;
}
else
{
*newpstr-- = *oldpstr;
}
oldpstr--;
}
}
int main()
{
char str[20] = "we are happy.";
replace_space(str);
printf("%s\n", str);
system("pause");
return 0;
}本文出自 “打印九九乘法表” 博客,请务必保留此出处http://10324228.blog.51cto.com/10314228/1686631
字符串替换空格:实现函数"we are happy."-->>"we%20are%20happy."
原文:http://10324228.blog.51cto.com/10314228/1686631