1. 程式人生 > >空格替換為“#25”

空格替換為“#25”

void ReplaceBlank(char *str,int len)
{
	assert(str != NULL);
	char *p = str;
	int count = 0;
	int charNum = 0;
	while(*p != '\0')
	{
		charNum++;
		if(*p == ' ')
		{
			count++;
		}
		p++;
	}//a b c   
	int newLen = count*2+charNum;//申請新的字元陣列長度
	if(newLen > len)
	{
		return;
	}
	int p2 = newLen;
	int p1 = charNum;
	//替換
	while(p2 > p1 && p1 >= 0)
	{
		if(str[p1] == ' ')//從後至前替換
		{
			str[p2--] = '5';
			str[p2--] = '2';
			str[p2--] = '#';
		}
		else
		{
			str[p2--] = str[p1];
		}
		p1--;
	}
}