C++:字符串刪除

輸入兩個字符串,從第一字符串中刪除第二個字符串中所有的字符。

例如,輸入”They are students.”和”aeiou”,則刪除之後的第一個字符串變成”Thy r stdnts.”

#include<string>
#include<iostream>
 
using namespace std;
 
int main()
{
    string first_str, second_str;
 
    getline(cin, first_str);
    getline(cin, second_str);
     
    int i, find_id;
    char data;

    for(i = 0; i < first_str.size();)
    {
        data = first_str.at(i);
        find_id = second_str.find(data, 0);
 
        if(find_id != string::npos)
        {
            first_str.erase(i, 1);
        }
        else
        {
            i++;
        }
    }
     
    cout << first_str.c_str() << endl;
     
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章