题目描述:
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
限制:
0 <= s 的长度 <= 10000
作者:Krahets
链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/50ywkd/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
解答:
class Solution {
public:
string replaceSpace(string s) {
string res;
for(auto c : s){
if(' ' == c){
res.push_back('%');
res.push_back('2');
res.push_back('0');
}
else
res.push_back(c);
}
return res;
}
};
运行结果: