zoukankan      html  css  js  c++  java
  • [Jobdu] 题目1510:替换空格

    题目描述:

    请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

    输入:

    每个输入文件仅包含一组测试样例。
    对于每组测试案例,输入一行代表要处理的字符串。

    输出:

    对应每个测试案例,出经过处理后的字符串。

    样例输入:
    We Are Happy
    样例输出:
    We%20Are%20Happy

    从后向前处理数组。

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 
     5 int main() {
     6     int count;
     7     int size;
     8     string s;
     9     while (getline(cin, s)) {
    10         count = 0;
    11         size = s.size();
    12         for (int i = 0; i < size; ++i) 
    13             if (s[i] == ' ')
    14                 ++count;
    15 
    16         s.resize(size + 2 * count);
    17 
    18         for (int i = size; i >= 0; --i) {
    19             if (s[i] == ' ') {
    20                 s[i + 2 * count] = '0';
    21                 s[i + 2 * count - 1] = '2';
    22                 s[i + 2 * count - 2] = '%';
    23                 --count;
    24             } else {
    25                 s[i + 2 * count] = s[i];
    26             }
    27         }
    28         cout << s << endl;
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    spring boot 报错 Failed to read HTTP message
    spring boot 之 Mybatis 配置
    Java生成xlsx格式的excel文件
    遍历FTP目录及下载
    Spring 报错
    git ssh key生成
    spring mvc原理
    LightOJ 1154
    Light OJ 1153
    入栈出栈的顺序问题
  • 原文地址:https://www.cnblogs.com/easonliu/p/3639073.html
Copyright © 2011-2022 走看看