Question: 3 Censored Copy ( 6 0 points ) Write a function named ReplacementCensor in a censor implementation and header file , that takes 3 function

3 Censored Copy (60 points)
Write a function named ReplacementCensor in a censor implementation and header file , that
takes 3 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", "Grouped-Letter-Unit"},
{"be", "wasp"},
{"not found", "not appearing"},
{"PlaCe", "Location"}
Expected ostream (Second argument) after being processed:
note: this is a line with multiple Grouped-Letter-Units that should wasp
reLoCationd.
all instances of Grouped-Letter-Unit eveninlargerGrouped-Letter-Unitsshould 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 bad_to_good {
{ "word" , "Grouped-Letter-Unit" },
{"be", "wasp" },
{ "not found" , "not appearing" },
{ "PlaCe" , "Location" }
};
std::istringstream iss("note: 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 = ReplacementCensor(iss, oss, bad_to_good);
std::set expected_return ={ "PLACE", "WORd", "Word", "be", "pLAce", "word" };
assert(result == expected_return);
assert(oss.str()== "note: this is a line with multiple Grouped-Letter-Units that should wasp reLoCationd./n all instances of Grouped-Letter-Unit eveninlargerGrouped-Letter-Unitsshould wasp reLoCationd.");
std::cout "All tests passed!" std::endl;
return 0;
}
censor.hpp
#pragma once
#include
#include
#include
#include
#include
std::set ReplacementCensor(std::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 toLowerCase(const std::string &str){
std::string lowerStr = str;
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
return lowerStr;
}
std::set ReplacementCensor(std::istream &input, std::ostream &output, const std::map &replacements){
std::set replacedWords;
std::string inputText((std::istreambuf_iterator(input)), std::istreambuf_iterator());
std::string lowerText = toLowerCase(inputText);
std::unordered_map lowerReplacements;
for (const auto &pair : replacements){
lowerReplacements[toLowerCase(pair.first)]= pair.second;
}
size_t pos =0;
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 (lowerText.substr(pos, lowerFrom.length())== lowerFrom){
// Replace in original text
inputText.replace(pos, lowerFrom.length(), to);
// Replace in lowerText for further comparisons
lowerText.replace(pos, lowerFrom.length(), toLowerCase(to));
// Add original case-sensitive key to replacedWords
replacedWords.insert(pair.first);
pos += to.length();
replaced = true;
break;
}
}
if (!replaced){
++pos;
}
}
output inputText;
return replacedWords;
}
I am getting this error: Assertion failed: result == expected_return, file .\main.cpp, line 23
 3 Censored Copy (60 points) Write a function named ReplacementCensor in

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!