Help Censoring Words!

Hey there, I am supposed to modify given code to receive input of sensitive words, receive input of a message, and then censor the whole sentence that contains that word. I am currently stuck on making the words actually censor, and I am not even to the point where I am supposed to modulize(sp?) the program =/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <fstream>
#include <string>

#include "sensitiveWords.h"
#include "message.h"


using namespace std;


int numSensitiveWords = 0;

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 (isAlphanumeric(message[pos] || sentencePunctuation(message[pos])))
    {
        if (pos > 0)
        {
            if (isAlphanumeric(message[pos-1]) || sentencePunctuation(message[pos-1]))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        return false;
    }
}


/*
 * Return the string comprising the word that begins at
 * position beginningAt.
 */
std::string extractWord (const std::string& fromMessage, int beginningAt)
{
    int x = 0;

    while(beginningAt + x <= fromMessage.length() && isAlphanumeric(fromMessage[beginningAt + x]))
    {
        x++;
    }
    string theWord = fromMessage.substr(beginningAt, x);
    return theWord;
}


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 start = findSentenceStart(msg, pos);
    int stop = findSentenceStop(msg, pos);
    for (start; start != stop; start++)
    {
        if(msg.at(start) == '\n')
        {
           //blank
        }
        else
        {
            msg.at(start) = '@';
        }
    }
}



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 models, "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);
	}
    }
}



void readSensitiveWords (istream& in)
{
  string word;
  while ((in >> word) && (word != "==="))
    addSensitiveWord (word);
  getline (in, word);
}


string readMessage (istream& in)
{
  string msg;
  string line;
  getline (in, line);
  while (in)
    {
      msg = msg + line + "\n";
      getline (in, line);
    }
  return msg;
}

int main()
{
  readSensitiveWords(cin);
  string msg = readMessage(cin);
  censorMessage (msg);
  cout << msg << flush;
  return 0;
}


I modified the functions wordBeginsAt, extractWord, and censorSentenceAt. The rest was given to me and is not to be touched. Any help or direction would be greatly appreciated.

Thanks!
-iTzBiGENGLAND
Oh, and the problem is that I can not replace the sentences containing sensitive words with @@@@@@@... and so on. That was probably important to state. =P
Topic archived. No new replies allowed.