#include "stdio.h"
#include "string.h"
#include <stdlib.h>
char* change(char *str)
{
//判断有多少空格
int i=0;
char *p=str;
while(*p)
{
if((*p)==‘ ‘)i++;
p++;
}
//申请新空间
char *temp=(char *)malloc(strlen(str)+3*i+1);
//开始置换
p=str;
int k=0;
while((*p))
{
if((*p)!=‘ ‘)
{
temp[k]=(*p);
}
else
{
temp[k]=‘%‘;k++;
temp[k]=‘2‘;k++;
temp[k]=‘0‘;
}
p++;
k++;
}
temp[k]=‘\0‘;
return temp;
}
int main()
{
char* out="i am a boy!";
out=change(out);
printf("%s\n",out);
return 0;
}
将空格替换成为“%20”的算法
原文:http://blog.csdn.net/itbuluoge/article/details/18552955