您的位置:首页 > 职场人生

剑指offer,面试题四替换空格

2016-11-02 11:26 232 查看
请实现一个函数,把字符数组中的每个空格替换成“%20”。

例如输入“we are happy.”,则输出“we%20are%20happy.”。

#include<stdio.h>

#include<string.h>

void replace_space(char arr[])

{

int count_space=0;

int i=0;

char *star=arr+strlen(arr)-1;

char *end=arr+strlen(arr)-1;

while (arr[i])

{

if (arr[i]==' ')

{

count_space++;

}

i++;

}

end=end+count_space*2;

while (end!=star)

{

if (*star==' ')

{

*end='0';

end--;

*end='2';

end--;

*end='%';

star--;

end--;

}

else

{

*end=*star;

end--;

star--;

}

}

}

int main()

{

char arr[30]={"we are happy."};

replace_space(arr);

printf("%s\n",arr);

system("pause");

return 0;

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