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