Question: 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 ReplacementCensor(std::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 toLower(const std::string& str){
std::string lowerStr = str;
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
return lowerStr;
}
// Helper function to replace text case-insensitively
void replaceText(std::string& text, const std::string& oldText, const std::string& newText, std::set& replacedWords){
std::string lowerOldText = toLower(oldText);
std::string lowerText = toLower(text);
size_t pos =0;
while ((pos = lowerText.find(lowerOldText, pos))!= std::string::npos){
// Capture the original case word
std::string originalWord = text.substr(pos, oldText.length());
replacedWords.insert(originalWord);
// Replace the word in the original text
text.replace(pos, oldText.length(), newText);
// Update lowerText to reflect the replacement without affecting future replacements
lowerText.replace(pos, oldText.length(), std::string(newText.length(),'*'));
pos += newText.length();
}
}
std::set ReplacementCensor(std::istream& is, std::ostream& os, const std::map& replacements){
std::set replacedWords;
std::string text((std::istreambuf_iterator(is)), std::istreambuf_iterator());
for (const auto& [key, value] : replacements){
replaceText(text, key, value, replacedWords);
}
os text;
return replacedWords;
}
main.cpp
#include "censor.hpp"
#include
#include
#include
int main(){
std::map replacements ={
{"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.
all instances of word eveninlargerWordsshould be rePLAced.");
std::ostringstream oss;
std::set result = ReplacementCensor(iss, oss, replacements);
std::set expected_return ={"PLACE", "WORds", "Word", "be", "rePLACEd", "word"};
std::string expected_output = "note: this is a line with multiple Grouped-Letter-Units that should wasp reLoCationed.
all instances of Grouped-Letter-Unit eveninlargerGrouped-Letter-Unitsshould wasp reLoCationed.
";
// Check the result
if (result != expected_return){
std::cout "Expected set: ";
for (const auto& s : expected_return) 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 (oss.str()!= expected_output){
std::cout "Expected output:
" expected_output;
std::cout "
Actual output:
" oss.str();
} else {
std::cout "Output stream comparison passed.
";
}
std::cout "All tests completed!" std::endl;
return 0;
}
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 Grouped-Letter-Units that should wasp reLoCationed.
all instances of Grouped-Letter-Unit eveninlargerGrouped-Letter-Unitsshould wasp reLoCationed.
Actual output:
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.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
 censor.hpp: #pragma once #include #include #include #include std::set ReplacementCensor(std::istream& is, std::ostream&

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!