Answered step by step
Verified Expert Solution
Question
1 Approved Answer
3 Censored Copy ( 6 0 points ) Write a function named ReplacementCensor in a censor implementation and header file , that takes 3 function
Censored Copy points
Write a function named ReplacementCensor in a censor implementation and header file that
takes function arguments. The first argument is an istream of text to be processed. The
second argument is an ostream to where the processed text should be copied to The last argument
is a map of strings to strings, where the key is a string that needs to be replaced with the
value in the text of the first argument's contents. This function should return a set containing
the words that were replaced. The keys should be matched in a case insensitive manner, and
the text to replace may not be white space delimited, meaning they can appear in longer words.
Example istream First argument:
note: this is a line with multiple WORds that should be rePLACEd.
all instances of word eveninlargerWordsshould be repLAced.
Example map Third argument:
word "GroupedLetterUnit"
be "wasp"
not found", "not appearing"
PlaCe "Location"
Expected ostream Second argument after being processed:
note: this is a line with multiple GroupedLetterUnits that should wasp
reLoCationd.
all instances of GroupedLetterUnit eveninlargerGroupedLetterUnitsshould wasp
reLoCationd.
Expected set returned by ReplacementCensor:
PLACE "WORd", "Word", be "pLAce", "word"
main.cpp
#include "censor.hpp
#include
#include
#include
#include
#include
#include
int main
std::map badtogood
"word" "GroupedLetterUnit"
be "wasp"
"not found" "not appearing"
"PlaCe" "Location"
;
std::istringstream issnote: this is a line with multiple WORds that should be rePLACEd.n all instances of word eveninlargerWordsshould be repLAced.";
std::ostringstream oss;
std::set result ReplacementCensoriss oss, badtogood;
std::set expectedreturn "PLACE", "WORd", "Word", be "pLAce", "word" ;
assertresult expectedreturn;
assertossstr "note: this is a line with multiple GroupedLetterUnits that should wasp reLoCationd.n all instances of GroupedLetterUnit eveninlargerGroupedLetterUnitsshould wasp reLoCationd.";
std::cout "All tests passed!" std::endl;
return ;
censor.hpp
#pragma once
#include
#include
#include
#include
#include
std::set ReplacementCensorstd::istream &input, std::ostream &output, const std::map &replacements;
censor.cpp
#include "censor.hpp
#include
#include
#include
#include
#include
Convert a string to lowercase
std::string toLowerCaseconst std::string &str
std::string lowerStr str;
std::transformlowerStrbegin lowerStr.end lowerStr.begin ::tolower;
return lowerStr;
std::set ReplacementCensorstd::istream &input, std::ostream &output, const std::map &replacements
std::set replacedWords;
std::string inputTextstd::istreambufiteratorinput std::istreambufiterator;
std::string lowerText toLowerCaseinputText;
std::unorderedmap lowerReplacements;
for const auto &pair : replacements
lowerReplacementstoLowerCasepairfirst pair.second;
sizet pos ;
while pos lowerText.size
bool replaced false;
for const auto &pair : lowerReplacements
const std::string &lowerFrom pair.first;
const std::string &to pair.second;
if lowerTextsubstrpos lowerFrom.length lowerFrom
Replace in original text
inputText.replacepos lowerFrom.length to;
Replace in lowerText for further comparisons
lowerText.replacepos lowerFrom.length toLowerCaseto;
Add original casesensitive key to replacedWords
replacedWords.insertpairfirst;
pos tolength;
replaced true;
break;
if replaced
pos;
output inputText;
return replacedWords;
I am getting this error: Assertion failed: result expectedreturn, file maincpp line
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started