您的位置:首页 > 其它

替换空格算法

2014-01-20 13:56 218 查看
#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”的算法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 替换空格