字符串的查找和替换

来源:百度知道 编辑:UC知道 时间:2024/05/12 16:17:23
问题描述:打开一篇英文文章,在该文章中找出所有给定的单词,然后对所有给定的单词替换为另外一个单词,再存盘。要用C或C++语言编辑

//打开的文件为*.txt的。实现了基本功能。不是很完善,没有考虑效
//率性能等问题。
void convert_text()
{
string file_name;
cout<<"input file name:";
cin>>file_name;
ifstream infile(file_name.c_str(),ios::in);
if(!infile)
{
cerr<<"unable to open file "<<file_name<<"--bailing out!\n";
exit( -1);
}
string filter0,filter1;
const char separate='\n';
cout<<"input the word you want to place:";
cin>>filter0;
cout<<"with anther word as:";
cin>>filter1;
string text_line;
vector<string> tempVector;
while(getline(infile,text_line,separate))
{
cout<<text_line<<endl;
int length = text_line.size();
string::size_type prev = 0;
string::size_type pos = 0;
while(string::npos != (pos=text_line.find_first_of(' ',pos)))
{