a function CensorString that takes two strings as input
and returns the first string with all of the characters that are present in the second removed
*/
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
ifstream in;
ofstream out;
string CensorString1(string text, string remove);
void CensorString2(string & text, string remove);
int maxNum(ifstream in);
int main(){
string openFileName;
cout << "enter the name: ";
cin >> openFileName;
in.open(openFileName);
string a;
string b;
/*课本上的例子*/
a = "Stanford University";
b = "Llamas like to laugh";
cout << a <<" "<< b<<endl;
string remove1 = "nt";
string remove2 = "la";
cout << CensorString1(a, remove1) << endl;
CensorString2(a, remove1);
cout << a << endl;
cout << CensorString1(b, remove2) << endl;
CensorString2(b, remove2);
cout << b << endl;
/*自己输入的*/
string c;
string d;
cin >> c;
cin >> d;
cout << CensorString1(c, d) << endl;
CensorString2(c, d);
cout << c;
}
int maxNum(ifstream in){
int maxN = 0;
int number1;
int number2;
stringstream ss;
string line;
/*get the first line*/
getline(in, line);
ss << line;
ss >> number1;
maxN = number1;
while(true){
getline(in, line);
ss << line;
ss >> number2;
if(number2 > maxN) maxN =number2;
if(in.eof())break;
}
return maxN;
}
string CensorString1(string text, string remove){
string result;
result = text;
for(int i = 0; i < remove.length(); i++){
for(int j = 0; j < text.length(); j++){
if(text[j] == remove[i]) result.erase(result.find(text[j], 0), 1);
}
}
return result;
}
void CensorString2(string & text, string remove){
for(int i = 0; i < remove.length(); i++){
while(true){
if (text.find(remove[i], 0) != string::npos) text.erase(text.find(remove[i],0), 1);
if (text.find(remove[i], 0) == string::npos) break;
}
}
}