Not sure which function is not quite right

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
  readSensitiveWords(cin);
  string msg = readMessage(cin);
  censorMessage(msg);
  cout << msg << flush;
  return 0;
}

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);
            }
        }
    }
}


These are the functions that I wrote. Something is causing it to pick up that word.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * 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;
        return false;
    }
    pos++;
    return true;
}

/*
 * 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!
1
2
3
4
5
6
7
8
9
10
11
12
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
        return false;
    }
    pos++; // as is this (pos is passed by value, 
           // so any changes disappear at return)
    return true;
}


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) = '@';
}
Last edited on
These functions were already defined.
1
2
3
4
5
6
7
8
9
10
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 == '!';
}


I actually had changed that function, sorry.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool wordBeginsAt (const std::string& message, int pos)
{
    while(pos < message.length())
    {
          if(isAlphanumeric(message[pos]) && sentencePunctuation(message[pos]))
        {
            --pos;
            return false;
        }
        else
        {
            ++pos;
            return true;
        }
    }
}
Last edited on
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, return false)
2. if no, can previous character separate words? (if yes, return true)

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])))
                return false;
        else
            return true;
    }
    return false 
}
Last edited on
Awesome! That was it. Thank you sooooo much!
Topic archived. No new replies allowed.