/*
* 请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
*
* 示例 1:
输入:s = "We are happy."
输出:"We%20are%20happy."
* */
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
string replaceSpace(string s) {
string str;
for(auto &c: s){
if(c == ' '){
str.push_back('%');
str.push_back('2');
str.push_back('0');
} else
str.push_back(c);
}
return str;
}
};
int main() {
std::cout << "Hello, World!" << std::endl;
string s="We are happy.";
Solution p;
cout<< p.replaceSpace(s);
return 0;
}
剑指offer--面试题05. 替换空格
最新推荐文章于 2024-08-16 18:12:44 发布