请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
class Solution {
public:string replaceSpace(string s) {int count=0;int len=s.size();string ans;//计算空格个数for(int i=0;i<len;i++){if(s[i]==' '){count++;}}ans.resize(len+2*count);//printf("%d",count);int j=0;for(int i=0;i<s.size();i++,j++){if(s[i]==' '){ans[j++]='%';ans[j++]='2';ans[j]='0';}else{ans[j]=s[i];}}return ans;}
};