【牛客】[编程题] 删除重读字符c++

1.题目描述

输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.

2.思路解析

  1. 先用getline连续输入字符
  2. ①遍历s2,找到和s1中相同的字符
  3. ②然后删除
  4. 最后打印s1

3.代码实现

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    string s1,s2;
    getline(cin, s1); // 连续输入字符
    getline(cin, s2);
    
    for(int i = 0; i < s2.size(); ++i)
    {
        int index ; // 找到s1里面的重复字符,删除
        while((index = s1.find(s2[i])) != -1)
              s1.erase(index, 1); // erase(size_t pos = 0;size_t len = npos)
    }

    cout<<s1<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43967449/article/details/106501145