首页 > 其他 > 详细

空格替换

时间:2015-11-21 22:53:02      阅读:405      评论:0      收藏:0      [点我收藏+]

字符串替换空格:请实现一个函数,把字符串中的每个空格替换成20”。例如输入“we are happy.”,则输出“we%20are%20happy.”

#include <stdio.h>

#include <assert.h>

 

void replace_black(char *str)

{

assert(str);

int black = 0;

int oldlen = strlen(str);

int newlen = 0;

char *tmp = str;

while (*tmp)

{

if (*tmp == ‘ ‘)

black++;

tmp++;

}

newlen = oldlen + 2 * black;

while (oldlen < newlen)

{

if (str[oldlen] != ‘ ‘)

{

str[newlen--] = str[oldlen--];

}

else

{

str[newlen--] = ‘0‘;

str[newlen--] = ‘2‘;

str[newlen--] = ‘%‘;

oldlen--;

}

}

}

int main()

{

char p[20] = "we are happy";

replace_black(p);

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

system("pause");

return 0;

}

 


本文出自 “1” 博客,请务必保留此出处http://10808695.blog.51cto.com/10798695/1715570

空格替换

原文:http://10808695.blog.51cto.com/10798695/1715570

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!