请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = "We are happy."
输出:"We%20are%20happy."
输出:"We%20are%20happy."
限制:
0 <= s 的长度 <= 10000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof
链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof
string可以直接用+的方式来添加字符
1 class Solution { 2 public: 3 string replaceSpace(string s) { 4 if(s.empty()) return s; 5 int len=s.size(); 6 string news; 7 for(int i=0;i<len;i++) 8 { 9 if(s[i]==' ') news+="%20"; 10 else news+=s[i]; 11 } 12 return news; 13 } 14 };