您的位置:首页 > 其它

04 替换空格

2016-05-19 22:00 351 查看
#include <stdio.h>

void replaceblank(char str[], int len) {
if (str == NULL || len<1)
{
return;
}

int reallong=0, blanks=0,i=0;
while (str[i]!='\0')
{
if (str[i]==' ')
{
blanks++;
}
reallong++;

i++;

}

int p1 = reallong, p2 = reallong + 2 * blanks;
if (p2>len)
{
return;
}
while (p1!=p2)
{
if (str[p1]==' ')
{
str[p2--] = '0';
str[p2--] = '2';
str[p2--] = '%';
}
else
{
str[p2--] = str[p1];
}
p1--;
}

}

void main() {

char a[100];
gets(a);
replaceblank(a, 100);
printf("%s", a);

}


测试结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: