C++工程师养成 每日一题

题目:

题目来源牛客网:https://www.nowcoder.com/practice/f0db4c36573d459cae44ac90b90c6212?tpId

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

解答:

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 int main(){
 6     string s1, s2;
 7     getline(cin, s1);
 8     getline(cin, s2);
 9     int a[256] = { 0 };
10     for (int i = 0; i < s2.size(); i++){
11         a[s2[i]]++;
12     }
13     string res;
14     for (int i = 0; i < s1.size(); i++){
15         if (a[s1[i]] == 0){
16             res += s1[i];
17         }
18     }
19     cout << res << endl;
20     return 0;
21 }

此题的解答关键是对String类接口的使用。

猜你喜欢

转载自www.cnblogs.com/Kaniso-Vok/p/11824014.html