首页 > 移动平台 > 详细

字符串替换空格:实现函数"we are happy."-->>"we%20are%20happy."

时间:2015-08-21 00:26:07      阅读:303      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

void replace_space(char *str)
{
	assert(str);
	char *pstr = str;
	int space = 0;
	int len = 0;
	int newlen = 0;
	while (*str)
	{
		if (*str == ‘ ‘)
			space++;
		len++;
		str++;
		
	}
	newlen = len + space * 2;
	char *newpstr = pstr + newlen - 1;
	char *oldpstr = pstr + len - 1;
	while (oldpstr < newpstr)
	{
		if (*oldpstr == ‘ ‘)
		{
			*newpstr-- = ‘0‘;
			*newpstr-- = ‘2‘;
			*newpstr-- = ‘%‘;
		}
		else
		{
			*newpstr-- = *oldpstr;
		}
		oldpstr--;
	}
}

int main()
{
	char str[20] = "we are happy.";
	replace_space(str);
	printf("%s\n", str);
	system("pause");
	return 0;
}


本文出自 “打印九九乘法表” 博客,请务必保留此出处http://10324228.blog.51cto.com/10314228/1686631

字符串替换空格:实现函数"we are happy."-->>"we%20are%20happy."

原文:http://10324228.blog.51cto.com/10314228/1686631

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