輸入字符串 s 和字符 c,刪除 s 中所有字符 c,輸出結果。多組測試數據。
讀取字符串和字符,遍歷字符串,遇到非 c 字符直接輸出。
#include
#include
using namespace std;
int main() {
string s;
while (cin >> s) {
char c; cin >> c;
for (char ch : s) if (ch != c) cout << ch;
cout << endl;
}
return 0;
}
利用標準庫算法 remove 將目標字符移至末尾,再 erase 刪除。
#include
#include
#include
using namespace std;
int main() {
string s, cstr;
while (cin >> s >> cstr) {
char c = cstr[0];
s.erase(remove(s.begin(), s.end(), c), s.end());
cout << s << endl;
}
return 0;
}
簡單的字符串處理題,考察基本輸入輸出與字符過濾。C++ STL 的 remove-erase 惯用法代碼簡潔高效。