Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with C++ program All files needed are provided at the bottom. Make sure to separate the files into sensitiveWords.cpp and message.cpp General Instructions:

Need help with C++ program

All files needed are provided at the bottom.

Make sure to separate the files into sensitiveWords.cpp and message.cpp

General Instructions: Read the problem description below and implement this program in C++. The files for this assignment are provided under the folder supporting-files for this assignment.

o All of the functions making up this program are complete and in working order except for those marked with //!!comments, which you must implement.

o The major challenge in this assignment is to divide the program into separately compiled modules.

A Message module, consisting of files message.h and message.cpp, containing code dealing specifically with manipulation of entire messages.

A SensitiveWords module, consisting of files sensitiveWords.h and sensitiveWords.cpp, containing code dealing specifically with maintaining a list of sensitive words and checking words from a message against that list.

There are some functions that are not specifically related to either of these modules, but that contain code needed by them. You should apportion these functions to the modules in a way consistent with the goals of high cohesion and low coupling.

Consult the comments in the provided code for additional details. Problem description: The Justice Department runs a Witness Protection Plan in which witnesses to crimes are given new identities to protect them from retaliation by the people against whom they testify in court. Experience hase shown that many of the protected witnesses are their own worst enemies - often giving away their new locations and identities in misguided attempts to contact relatives and friends and to assure them that all is well. The Department is experimenting with a new idea of allowing such communications, in email form only, with the idea that employees will inspect the communications first and cut out any potentially dangerous sentences, then send the email from the Justice Department computers so that they cannot be traced back to the witnesses. Unfortunately, the budget for the pilot project was cut almost as soon as the project commenced. There is insufficient money to actually hire people to read all the email, so an automated solution is sought instead. Write a C++ program that, given a list of sensitive words and a message (in plain text form), scans the message for any sentence containing a sensitive word (ignoring differences in upper/lower case). If a sensitive word is found, every character in that sentence (except for line terminators) should be replaced by '@' characters. For the purposes of this program, a word is a string of consecutive alphanumeric characters bounded in the message by the start or end of the message, and/or by any non-alphanumeric character. A sentence is a string of consecutive words bounded by the start or end of the message, by a paragraph boundary (a line containing zero characters), and/or by one of the punctuation characters: '.', '?', or '!'.

Input

Input to the program is taken from the standard input (cin). You will need to change this to make your program read the input data from the text file input.txt. Please notice that the input consists of a word list and a message.

A word list consists of zero or more words, one per line and left-justified. Words may be up to 40 characters in length. The end of the word list is signalled by a line containing only the characters === (three equal signs).

The word list is immediately followed by a message. A message consists of zero or more lines of text containing up to 80 characters per line. The end of the message is signaled by the end of the input. (Note: when typing input, you can signal end of input with a Ctrl-Z in Windows or a Ctrl-D in Unix/Linux.

image text in transcribed

Output

Print the message, exactly as it appears in the input except for replacement of sentences by '@' characters as described above. Save your output into a file named output.txt

image text in transcribed

Notes

It is important to note that the output must match the given format exactly. In all assignments, whenever I speak of a line of output, that line must be properly terminated (by " " or endl).

Files needed:

censor.cpp

#include

#include

#include

using namespace std;

int numSensitiveWords = 0;

const int MaxSensitiveWords = 5000;

std::string sensitiveWords[MaxSensitiveWords];

/*

*returns a string converted to lowercase

*/

std::string toLowerCase (std::string word)

{

string result = word;

for (int i = 0; i

if (word[i] >= 'A' && word[i]

result[i] = result[i] + 'a' - 'A';

return result;

}

/*

*Return true if char is alpha numeric

*/

bool isAlphanumeric (char c)

{

return (c >= 'A' && c

|| (c >= 'a' && c

|| (c >= '0' && c

}

/*

*returns true if character is punctuation

*/

bool sentencePunctuation (char c)

{

return c == '.' || c == '?' || c == '!';

}

/*

* Return true if the character at position pos is the first character

* of a word.

*/

bool wordBeginsAt (const std::string& message, int pos)

{

//!! Fill in the body for this function

return true; //!! Bogus return statement to permit compilation - replace

}

/*

* Return the word that begins at

* position beginningAt as a string

*/

std::string extractWord (const std::string& fromMessage, int beginningAt)

{

//!! Fill in the body for this function

return ""; //!! Bogus return statement to permit compilation - replace

}

/*

* Returns true if pos is the index of the beginning of the message

*or if pos is the index of the beginning of a paragraph.

*/

bool paragraphStart (const string& msg, int pos)

{

if (pos == 0)

return true;

if (pos == 1)

return false;

return msg[pos-1] == ' ' && msg[pos-2] == ' ';

}

/*

* Returns the index of the first character of the sentence containing

*the index pos

*/

int findSentenceStart (const string& msg, int pos)

{

while (pos > 0 && !paragraphStart(msg,pos) && !sentencePunctuation(msg[pos]))

--pos;

if (sentencePunctuation(msg[pos]))

++pos;

return pos;

}

/*returns true if the index pos has reached the end of a paragraph

*or the end of the message

*/

bool paragraphEnd (const string& msg, int pos)

{

if (pos == msg.length())

return true;

if (pos == msg.length()-1)

return false;

return msg[pos] == ' ' && msg[pos+1] == ' ';

}

/*

* Returns the index of the last character of the sentence

*containing the index of pos

*/

int findSentenceStop (const string& msg, int pos)

{

while (pos

&& !paragraphEnd(msg,pos)

&& !(pos > 0 && sentencePunctuation(msg[pos-1])))

++pos;

return pos;

}

/*

* Replace all characters of a sentence containing the index pos with '@'

* except for ''.

*/

void censorSentenceAt (string& msg, int pos)

{

//!! Fill in the body for this function

}

/*checks to see if a word is in the list of

*sensitive words

*/

bool isSensitive(std::string& wordFromMessage)

{

string word = toLowerCase(wordFromMessage);

for (int i = 0; i

if (word == sensitiveWords[i])

return true;

return false;

}

/*

*adds a word to the list of sensitive words, if it is not already

*in the list of Sensitive words

*/

void addSensitiveWord (std::string& wordFromMessage)

{

string word = toLowerCase(wordFromMessage);

if (!isSensitive(word))

{

sensitiveWords[numSensitiveWords] = word;

++numSensitiveWords;

}

}

///////////////////////////////////////////////////////

//!!

// The 4 functions below should remain in censor.cpp

// The functions and data above should be used to create

// two models, "sensistiveWords" and "message". Keep the

// cohesion high and the coupling low.

/*

*censors message in msg

*/

void censorMessage (string& msg)

{

for (int i = 0; i

{

if (wordBeginsAt(msg, i))

{

string word = extractWord(msg, i);

if (isSensitive(word))

censorSentenceAt (msg, i);

}

}

}

/*

*reads sensitive words from file stream in

*/

void readSensitiveWords (istream& in)

{

string word;

while ((in >> word) && (word != "==="))

addSensitiveWord (word);

getline (in, word);

}

/*

*reads message from file stream in

*/

string readMessage (istream& in)

{

string msg;

string line;

getline (in, line);

while (in)

{

msg = msg + line + " ";

getline (in, line);

}

return msg;

}

int main()

{

readSensitiveWords(cin);

string msg = readMessage(cin);

censorMessage (msg);

cout

return 0;

}

message.cpp

#include "message.h"

sensitiveWords.cpp

#include "sensitiveWords.h"

message.h

#ifndef MESSAGE_H

#define MESSAGE_H

// Functions for manipulating a message

//

// A message in this case is a single long string

// containing characters separating the lines.

#endif

sensitiveWords.h

#ifndef SENSITIVEWORDS_H_INCLUDED

#define SENSITIVEWORDS_H_INCLUDED

#endif // SENSITIVEWORDS_H_INCLUDED

Given the following input which is provided in the input.txt file: John Jane Smith Jones Kansas court crime phone Dear Mom I just wanted to let you know that I am alive and well. Jane is well also. T'm glad they were able to relocate us. My only complaint is that I wish they could have found someplace more exciting than Kansas for us to live in! If you really need to contact us, you can do so by telephone. The number i:s (757) 555-0478, but don't tell anyone Love the new John Smith

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

1. What are your creative strengths?

Answered: 1 week ago

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago