请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
class Solution {
public:
void replaceSpace(char *str,int length) {
if(str==NULL || length<=0)
{
return;
}
int i=0;
int oLen=0;
int empty=0;
while(*(str+i)!='\0')
{
oLen++;
if(*(str+i)==' ')
{
empty++;
}
i++;
}
int Len=oLen+2*empty;
int m=Len;
for(int j=oLen;j>=0;j--)
{
if(*(str+j)==' '){
*(str+(m--))='0';
*(str+(m--))='2';
*(str+(m--))='%';
}
else
*(str+(m--))=*(str+j);
}
}
};