Section Handout #1




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;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值