I need to cut up this code and make a header file and a new cpp file. I need a message.cpp and a message.h containing code dealing specifically with manipulation of entire messages. I dont know which functions to move to where
const int MaxSensitiveWords = 5000;
std::string sensitiveWords[MaxSensitiveWords];
std::string toLowerCase (std::string word)
{
string result = word;
for (int i = 0; i < word.length(); ++i)
if (word[i] >= 'A' && word[i] <= 'Z')
result[i] = result[i] + 'a' - 'A';
return result;
}
bool isAlphanumeric (char c)
{
return (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9');
}
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)
{
if (pos == 0)
return true;
/*
* Return the string comprising the word that begins at
* position beginningAt.
*/
std::string extractWord (const std::string& fromMessage, int beginningAt)
{
int len = 0;
while (beginningAt + len < fromMessage.length()
&& isAlphanumeric(fromMessage[beginningAt+len]))
{
++len;
}
string result = fromMessage.substr(beginningAt, len);
return result;
}
bool paragraphStart (const string& msg, int pos)
{
if (pos == 0)
return true;
if (pos == 1)
return false;
return msg[pos-1] == '\n' && msg[pos-2] == '\n';
}
/*
* Return the position that denotes the first character of the sentence
* that begins at or before position pos and that ends at or after
* that position.
*/
int findSentenceStart (const string& msg, int pos)
{
while (pos > 0 && !paragraphStart(msg,pos) && !sentencePunctuation(msg[pos]))
--pos;
if (sentencePunctuation(msg[pos]))
++pos;
return pos;
}
bool paragraphEnd (const string& msg, int pos)
{
if (pos == msg.length())
return true;
if (pos == msg.length()-1)
return false;
return msg[pos] == '\n' && msg[pos+1] == '\n';
}
/*
* Return the position that denotes the last character of the sentence
* that begins at or before position pos and that ends at or after
* that position.
*/
int findSentenceStop (const string& msg, int pos)
{
while (pos < msg.length()
&& !paragraphEnd(msg,pos)
&& !(pos > 0 && sentencePunctuation(msg[pos-1])))
++pos;
return pos;
}
/*
* Replace by '@' all characters (except '\n') of the sentence
* that begins at or before position pos and that ends at or after
* that position.
*/
void censorSentenceAt (string& msg, int pos)
{
int first;
int last;
for (int loc = first; loc <= last; loc++)
if (msg[loc]!='\n')
msg[loc]='@';
}
bool isSensitive(std::string& wordFromMessage)
{
string word = toLowerCase(wordFromMessage);
for (int i = 0; i < numSensitiveWords; ++i)
if (word == sensitiveWords[i])
return true;
return false;
}
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 modules, "sensistiveWords" and "message". Keep the
// cohesion high and the coupling low.
void censorMessage (string& msg)
{
for (int i = 0; i < msg.length(); ++i)
{
if (wordBeginsAt(msg, i))
{
string word = extractWord(msg, i);
if (isSensitive(word))
censorSentenceAt (msg, i);
}
}
}
This is a declaration: void addSensitiveWord (std::string& wordFromMessage);
With that you declare that a function with that name and the specified parameters exists... somewhere.
A header is basically a collection of such declarations, which allows you to use these functions in translation units other than the one that contains the actual implementation of the function.