这道题有C风格字符串的解答,很巧妙,但是我还是喜欢C++的风格的解法,简单明了:
class Solution {
public:
void replaceSpace(char *str,int length) {
if(str == NULL || length <=0)
return;
string result = "";
int i=0;
while(str[i] != '\0'){
if(str[i] == ' '){
result += "%20";
}else{
result += str[i];
}
i++;
}
result.copy(str,result.length());
str[result.length()]='\0';
}
};