请实现一个函数,把字符串中的每一个空格替换成“%20”,比如输入
“We are Happly。” 则输出“we%20are%20happy。
”
void ReplaceBlank(char String[],int length)
{
//originalLength 为字符串string的实际长度
//length字符串的总容量
int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
while(string[i]!=‘\0‘)
{
++originalLength;
if(string[i] == ‘‘){++numberOfBlank;}
++i;
}
int newlength = originalLength+numberOfBlank;
if(newlength>length)
{return;}
int indexOfOriginal = originalLength;
int indexOfNew = newlength;
whlie(indexOfOriginal>=0 && indexOfNew>indexOfOriginal)
{
if(indexOfOriginal==‘ ‘)
{
string[indexOfNew--]=‘0‘;
string[indexOfNew--]=‘2‘;
string[indexOfNew--]=‘%‘;
}else
{string[indexOfNew--] = string[indexOfOriginal];}
--indexOfOriginal;
}
}