|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 糖逗 于 2020-3-31 17:12 编辑
题目描述:- 请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
-  
- 示例 1:
- 输入:s = "We are happy."
- 输出:"We%20are%20happy."
-  
- 限制:
- 0 <= s 的长度 <= 10000
- 来源:力扣(LeetCode)
- 链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof
- 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码
- #include <string>
- #include <iostream>
- using namespace std;
- string solution(string input){
- string result;
- for(int i = 0; i < input.size(); i++){
- if(input[i] == ' '){
- result += "%20";
- }
- else{
- result += input[i];
- }
- }
- return result;
-
- }
- int main(void){
- string input;
- getline(cin, input);
- cout << input << endl;
- string res = solution(input);
- cout << res << endl;
- return 0;
- }
复制代码
注意事项:
1.C++中输入带空格的string使用getline函数。 |
|