C++ 剑指offer 替换空格
替换空格
http://www.nowcoder.com/questionTerminal/4060ac7e3e404ad1a894ef3e17650423
char *str => str!=nullptr
char str[] => str!=NULL
class Solution {
public:void replaceSpace(char str[],int length) {
if(str == NULL && length<=0){
return;
}
int originLength = 0;
int numofbank = 0;
int i = 0;
while(str[i]!='\0'){
++ originLength;
if(str[i]==' '){
++ numofbank;
}
++ i;
}
int curlength = originLength + numofbank*2;
if(curlength>length){
return;
}
int indexOrigin = originLength;
int indexCur = curlength;
while(indexOrigin>=0 && indexCur >indexOrigin){
if(str[indexOrigin]== ' '){
str[indexCur--] = '0';
str[indexCur--] = '2';
str[indexCur--] = '%';
}
else{
str[indexCur--] = str[indexOrigin];
}
-- indexOrigin;
}
}
};