Create a text fi le that contains the text “I hate C++ and hate programming!” Write a program that reads in the text from the fi le and outputs each word to the console but replaces any occurrence of “hate” with “love.” Your program should work with any line of text that contains the word “hate,” not just the example given in this problem.
//Include header files
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//main function
int main( )
{
string inputText;
fstream inputFile;
//Open input file data
inputFile.open("file.txt");
//Replaces any occurrence of “hate”
//with “love.”
while (inputFile >> inputText)
{
if (inputText == "hate")
cout << "love ";
else
cout << inputText << " ";
}
inputFile.close();
cout << endl;
}
file.txt data:
I hate C++ and hate programming!
Output of the program code:
I love C++ and love programming!