题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
时间限制:1秒 空间限制:32768K 热度指数:559991
本题知识点: 字符串
class Solution {
public:
void replaceSpace(char *str,int length) {
int i=0;
int oldnum=0,newnum=0,basenum=0;
while(str[i]!='\0'){
oldnum++;
if(str[i]==' '){
basenum++;
}
i++;
}
newnum=oldnum+2*basenum;
int p1=oldnum,p2=newnum;
if(newnum>length)
return;
while(p1>=0&&p1<p2){
if(str[p1]==' '){
str[p2--]='0';
str[p2--]='2';
str[p2--]='%';
}
else
str[p2--]=str[p1];
p1--;
}
}
};