替换空格

请实现一个函数,把字符串中的每个空格替换成"%20"

 
class Solution {
public:
    string replaceSpaces(string &s) {
        string ans = "";
        for (char c : s) {
            if (c == ' ') {
                ans += "%20";
            }
            else ans += c;
        }
        
        return ans;
    }
};

  

posted @ 2022-12-10 15:31  !&&||  阅读(43)  评论(0)    收藏  举报