16.替换空格 (c语言)
样例
输入:"We are happy."
输出:"We%20are%20happy."
算法(c语言)
C++ 代码
class Solution {
public:
string replaceSpaces(string &str) {
int n=sizeof(str);
char a[10100]={0};//这个数组长度要大才行,如果小的话万一他有很多个空格。。
for(int i=0,x=0;str[i];i++){
if(str[i]==' ') {
a[x++]='%';
a[x++]='2';
a[x++]='0';
}
else a[x++]=str[i];
}
return a;
}
};