[Solved] Replace a particular word with another one in a file [closed]


As far as I understand you are comfortable with replacing words in a single paragraph and that you are doubtful regarding text with multiple paragraphs.

Please look into a function called “getline()” function.

This function reads entire your text until it encounters a “\n” element (Next line)

So you can use this getline function to get one whole paragraph into a string.

Using this getline function in a while loop allows you to get all the paragraphs from the text file

An example code has been provided below

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
    string a,b;
    a="he";
    b="she";
    fstream text("text.txt");
    string line;
    while (!text.eof( ))
    {
        getline(text,line);
        cout<<line<<endl;
        //This string "line" is basically a string containing your first paragraph
        //ADD your find and replace code here for the string "line".
        //The second time the while loop executes the string "line" will contain the second paragraph and so on..
    }
}
}

6

solved Replace a particular word with another one in a file [closed]