zoukankan      html  css  js  c++  java
  • 蓝桥杯 字符删除 字符串操作

    问题描述
      编写一个程序,先输入一个字符串str(长度不超过20),再输入单独的一个字符ch,然后程序会把字符串str当中出现的所有的ch字符都删掉,从而得到一个新的字符串str2,然后把这个字符串打印出来。
      输入格式:输入有两行,第一行是一个字符串(内部没有空格),第二行是一个字符。
      输出格式:经过处理以后的字符串。
    输入输出样例
    样例输入
    123-45-678
    -
    样例输出
    12345678
    以为是个水题,第一版代码。
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int main() {
     4     string s1, s2;
     5     cin >> s1 >> s2;
     6     for (int i = 0; i < s1.length(); i++) {
     7         if (s1[i] == s2[0]) {
     8             continue;
     9         } else {
    10             cout << s1[i];
    11         }
    12     }
    13     cout << endl;
    14     return 0;
    15 }

    然后发现错了一个样例

     样例7的数据

    就是第一个字符串是空串,第二个字符随意,然后输出空串。这样的话,因为cin的原因,第一版代码就是把a赋值给了s1,然后一直等待s2的输入。

    用getline直接读取一行就好了。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int main() {
     4     string s1, s2;
     5     getline(cin, s1);
     6     getline(cin, s2);
     7     for (int i = 0; i < s1.length(); i++) {
     8         if (s1[i] == s2[0]) {
     9             continue;
    10         } else {
    11             cout << s1[i];
    12         }
    13     }
    14     cout << endl;
    15     return 0;
    16 }
  • 相关阅读:
    Cufon css3@font-face
    HTML5 Canvas
    HTML5 Canvas 的宽高
    :nth-child()
    new Image()
    ios有些机型input和fixed导致的页面错位问题
    使用performance进行前端性能监控
    throttle(节流)和debounce(防抖)
    object-fit/object-position
    flex布局与ellipsis冲突问题
  • 原文地址:https://www.cnblogs.com/fx1998/p/12679129.html
Copyright © 2011-2022 走看看