https://www.nowcoder.com/questionTerminal/4060ac7e3e404ad1a894ef3e17650423
class Solution {
public:
void replaceSpace(char *str,int length) {
if(str==NULL||length<=0)
{
return;
}
int oldlen=0;
int num=0;
int i=0;
while(str[i]!='\0')
{
oldlen++;
if(str[i]==' ')
{
num++;
}
++i;
}
int newlen=oldlen+2*num;
if(newlen>length)
{
return;
}
int index=oldlen;
int indey=newlen;
while(index>=0&&indey>index)
{
if(str[index]==' ')
{
str[indey--]='0';
str[indey--]='2';
str[indey--]='%';
}
else
{
str[indey--]=str[index];
}
index--;
}
}
};