Its a program to read 'sensitive' words, then read a message and censor out the sentences with sensitive words. One of the sensitive words is 'phone'. The sentence it is censoring contains the word 'telephone'. It should not censor that word. Its one of these functions that is causing the problem but I just can't figure out which one.
This is the main program. This was already written and should not be changed.
/*
* 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 start = findSentenceStart(msg, pos);
int stop = findSentenceStop(msg, pos);
for (start; start != stop; start++)
{
if(msg.at(start) == '\n')
{
msg.at(start) = '\n';
}
else
{
msg.at(start) = '@';
}
}
}
/*
* Return true if the character at position pos is the first character
* of a word.
*/
bool wordBeginsAt (const std::string& message, int pos)
{
while (isAlphanumeric(message[pos]) && sentencePunctuation(message[pos]))
{
--pos;
returnfalse;
}
pos++;
returntrue;
}
/*
* Return the string comprising the word that begins at
* position beginningAt.
*/
std::string extractWord (const std::string& fromMessage, int beginningAt)
{
int i = 0;
while (beginningAt + i <= fromMessage.length() && isAlphanumeric(fromMessage[beginningAt + i]))
{
++i;
}
string result = fromMessage.substr(beginningAt, i);
return result;
}
I've gone through it over and over and I cannot figure it out. Please help!
bool wordBeginsAt (const std::string& message, int pos)
{
// are BOTH conditions in the loop possible at once (you used &&)?
while (isAlphanumeric(message[pos]) && sentencePunctuation(message[pos]))
{
--pos; //this is of no significance
returnfalse;
}
pos++; // as is this (pos is passed by value,
// so any changes disappear at return)
returntrue;
}
How is that supposed to work? The while loop only executes once. And I don't know what sentencePunctuation(message[pos]) returns, but it seems to me that wordBeginsAt() will always return true for first letter of a word as well as for any letter in the string.
As a sidenote:
1 2 3 4 5 6 7 8 9
if(msg.at(start) == '\n')
{
msg.at(start) = '\n'; // this is pretty redundant, no?
}
else
{
msg.at(start) = '@';
}
In your edit, how is line 8 accessible? It is not, hence the function always returns true. At some point it causes isSensitive(word) to receive an ending part of censored word. Also, read my comments to your code in my previous post.
EDIT: wordBeginsAt () only needs two checks:
1. can given character separate words? (if yes, returnfalse)
2. if no, can previous character separate words? (if yes, returntrue)
1 2 3 4 5 6 7 8 9 10 11 12
bool wordBeginsAt (const std::string& message, int pos)
{
if ( isAlphanumeric(message[pos] || sentencePunctuation(message[pos]))) // Note || not &&
{
if(pos > 0) // you need this not to crash at index 0
if(isAlphanumeric(message[pos-1] || sentencePunctuation(message[pos-1])))
returnfalse;
elsereturntrue;
}
returnfalse
}