Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 527 Bytes

替换空格.md

File metadata and controls

30 lines (23 loc) · 527 Bytes

题目

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

你可以假定输入字符串的长度最大是 1000。

注意输出字符串的长度可能大于 1000。

样例

输入:"We are happy."

输出:"We%20are%20happy."

参考答案

class Solution {
public:
    string replaceSpaces(string &str) {
        string res;
        for (auto x : str)
            if (x == ' ')
                res += "%20";
            else
                res += x;
        return res;
    }
};