Answered step by step
Verified Expert Solution
Question
1 Approved Answer
censor.hpp: #pragma once #include #include #include #include std::set ReplacementCensor ( std::istream& is , std::ostream& os , const std::map& replacements ) ; censor.cpp: #include censor.hpp
censor.hpp:
#pragma once
#include
#include
#include
#include
std::set ReplacementCensorstd::istream& is std::ostream& os const std::map& replacements;
censor.cpp:
#include "censor.hpp
#include
#include
#include
Helper function to convert a string to lowercase
std::string toLowerconst std::string& str
std::string lowerStr str;
std::transformlowerStrbegin lowerStr.end lowerStr.begin ::tolower;
return lowerStr;
Helper function to replace text caseinsensitively
void replaceTextstd::string& text, const std::string& oldText, const std::string& newText, std::set& replacedWords
std::string lowerOldText toLoweroldText;
std::string lowerText toLowertext;
sizet pos ;
while pos lowerText.findlowerOldText pos std::string::npos
Capture the original case word
std::string originalWord text.substrpos oldText.length;
replacedWords.insertoriginalWord;
Replace the word in the original text
text.replacepos oldText.length newText;
Update lowerText to reflect the replacement without affecting future replacements
lowerText.replacepos oldText.length std::stringnewTextlength;
pos newText.length;
std::set ReplacementCensorstd::istream& is std::ostream& os const std::map& replacements
std::set replacedWords;
std::string textstd::istreambufiteratoris std::istreambufiterator;
for const auto& key value : replacements
replaceTexttext key, value, replacedWords;
os text;
return replacedWords;
main.cpp
#include "censor.hpp
#include
#include
#include
int main
std::map replacements
word "GroupedLetterUnit"
be "wasp"
not found", "not appearing"
PlaCe "LoCation"
;
std::istringstream issnote: this is a line with multiple WORds that should be rePLACEd.
all instances of word eveninlargerWordsshould be rePLAced.";
std::ostringstream oss;
std::set result ReplacementCensoriss oss, replacements;
std::set expectedreturn PLACE "WORds", "Word", be "rePLACEd", "word";
std::string expectedoutput "note: this is a line with multiple GroupedLetterUnits that should wasp reLoCationed.
all instances of GroupedLetterUnit eveninlargerGroupedLetterUnitsshould wasp reLoCationed.
;
Check the result
if result expectedreturn
std::cout "Expected set: ;
for const auto& s : expectedreturn std::cout s ;
std::cout
Actual set: ;
for const auto& s : result std::cout s ;
std::cout
;
else
std::cout "Set comparison passed.
;
Check the output stream
if ossstr expectedoutput
std::cout "Expected output:
expectedoutput;
std::cout
Actual output:
oss.str;
else
std::cout "Output stream comparison passed.
;
std::cout "All tests completed!" std::endl;
return ;
This is all my code output:
Expected set: PLACE WORds Word be rePLACEd word
Actual set: PLACE PLAce WORd Word be word
Expected output:
note: this is a line with multiple GroupedLetterUnits that should wasp reLoCationed.
all instances of GroupedLetterUnit eveninlargerGroupedLetterUnitsshould wasp reLoCationed.
Actual output:
note: this is a line with multiple GroupedLetterUnits that should wasp reLoCationd.
all instances of GroupedLetterUnit eveninlargerGroupedLetterUnitsshould wasp reLoCationd.All tests completed!
My actual output missing e in reLoCationed. Could some one manually to check my code and help me figure it out the issuse thank you
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