Please implement a function to replace spaces in a string with "%20". For example, when the string is We Are Happy., the replaced string is We%20Are%20Happy.
class Solution {
public:
void replaceSpace(char *str,int length) {
if (str==NULL||length<0)
return ;
int numberofBlank=0,strlen=0;
int i=0;
while(str[i]!='\0')
{
++strlen;
if (str[i]==' ')
++numberofBlank;
++i;
}
int newlen=strlen+numberofBlank*2;
if (newlen>length)
return ;
while(strlen>=0&&newlen>strlen)
{
if (str[strlen]==' ')
{
str[newlen--]='0';
str [newlen -] = '2';
str[newlen--]='%';
}
else
str[newlen--]=str[strlen];
--strlen;
}
}
};