Program editing one sentence it shouldn't. I'm stuck!

I have an assignment to take a program and divide it into header files. There were a couple of functions I had to fill in too. The program is supposed to recognize certain words and replace each character of the sentence with '@'. Well, I have it divided, and the functions are written. It almost works perfectly with the exception of one sentence.

Given the input

John
jane
Smith
Jones
Kansas
court
crime
phone
555
===
Dear Mom,

I just wanted to let you know that I am alive and well. Jane is
well also. I'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 is
(757) 555-0478, but don't tell anyone.

Love,
the new John Smith


the output should be

Dear Mom,

I just wanted to let you know that I am alive and well.@@@@@@@@
@@@@@@@@@@ I'm glad they were able to relocate us.@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ If you really need
to contact us, you can do so by telephone.@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@
@@@@@@@@@@@@@@@@@@

My program is replacing the "If you really need to..."

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

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) = '@';
         }
    }
}


I wrote the censorSentenceAt function. The rest was already set up.

Please let me know if I am missing something. I don't understand why its editing that sentence too.
Last edited on
I think it may be something in this function. Like its not reading the beginning of the word correctly. isAlphanumeric and sentencePunctuation were given functions. I had to fill in the body of wordsBeginAt.
1
2
3
4
5
6
7
8
9
10
bool wordBeginsAt (const std::string& message, int pos)
{
    while (isAlphanumeric(message[pos]) && sentencePunctuation(message[pos]))
    {
        --pos;
        return false;
    }
    pos++;
    return true;
}
Last edited on
Topic archived. No new replies allowed.