题目描述
class Solution {
public:
void replaceSpace(char *str,int length) {
if(str==NULL||length<=0)
return;
int space=0,i=0;
while(i<length)
{
if(str[i]==' ')
++space;//空格数量
++i;
}
int newlength=length+2*space;//替换后字符串的长度
char *temp=new char[newlength+1];
i=length-1;
int j=newlength-1;
while(i>=0)
{
if(str[i]!=' ')
temp[j]=str[i];
else
{
temp[j--]='0';
temp[j--]='2';
temp[j]='%';
}
i--;j--;
}
temp[newlength]='\0';
strcpy(str,temp);
}
};