Unnecessary Censorship2. It’s easy: 1. Read in a text file. 2. Replace all 4 let
ID: 3779479 • Letter: U
Question
Unnecessary Censorship2. It’s easy: 1. Read in a text file. 2. Replace all 4 letter words with ****. 3. Write the result back out to a new file. For example, text.txt contains the text: Hullabaloo, Caneck! Caneck! Hullabaloo, Caneck! Caneck! Good-bye to texas university So long to the orange and the white Good luck to dear old Texas Aggies They are the boys that show the real old fight "The eyes of Texas are upon you" That is the song they sing so well Invokethecensor function: >> censor('text.txt'); The file text_censored.txt contains the censored text: Hullabaloo, Caneck! Caneck! Hullabaloo, Caneck! Caneck! Good-bye to texas university So **** to the orange and the white **** **** to **** old Texas Aggies **** are the **** **** **** the **** old fight "The **** of Texas are **** you" **** is the **** **** **** so **** Note: the output file name is simply the input filename with _censored appended to the end. >> in = 'text.txt'; >> out = [in(1:end-4) '_censored' in(end-3:end)] out = text_censored.txt
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int main ()
{
// Open a file "ReadMe.txt"
std::ifstream ifsInputFile ("wrd");
ofstream outfile;
outfile.open("result");
if (ifsInputFile.is_open ())
{
std::string strLine;
// Get every line
while (std::getline (ifsInputFile, strLine))
{
// Use std::stringstream to isolate words using operator >>
std::stringstream ssWordsBuf (strLine);
std::string strWord;
while (ssWordsBuf >> strWord){
if(strWord.length() == 4){
// write to file
outfile<< " "<< "****" << " ";
}
else{
outfile << " "<< strWord << " ";
}
}
}
ifsInputFile.close ();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.