Answered step by step
Verified Expert Solution
Link Copied!

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 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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

More Books

Students also viewed these Databases questions

Question

Explain global human resource management.

Answered: 1 week ago

Question

Describe the grievance procedure in a union environment.

Answered: 1 week ago

Question

Discuss whistleblower protection under OSHA.

Answered: 1 week ago