title: C++ writing a function that extracts words from a paragraph
body: <p>The program I am writing reads a text file, breaks the paragraph into individual words, compares them to a list of "sensitive words" and if a word from the text file matches a word from the Sensitive word list, it is censored. I have wrote functions that find the beginning of each word, and a function that will censor or replace words on the Sensitive word list with "@@@@@" (which I left out of this post). A word in this case is any string that contains alphanumeric characters. </p>
  
  <p>The function I am having trouble with is the function that will "extract" or return the individual words to compare to the sensitive word list (extractWord). At the moment it just returns the first letter of the last word in the sentence. So right now all the function does is return "w". I need all the individual words.</p>
  
  <p>Here is what I have so far ...</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  bool wordBeginsAt (const std::string&amp; message, int pos); 
  bool isAlphanumeric (char c);  // 
  std::string extractWord (const std::string&amp; fromMessage, int beginningAt);
  
  int main()
  {
      string word = "I need to break these words up individually. 12345 count as words";
      string newWord;
  
      for (int i = 0; i &lt; word.length(); ++i)
      {
          if (wordBeginsAt(word, i))
          {
              newWord = extractWord(word, i);
          }
      }
  
      //cout &lt;&lt; newWord;   // testing output
  
      return 0;
  }
  
  bool wordBeginsAt (const std::string&amp; message, int pos)
  {
      if(pos==0)
          {return true;}
      else
          if (isAlphanumeric(message[pos])==true &amp;&amp; isAlphanumeric(message[pos-  1])==false)
          {
              return true;
          }
      else
          return false;
  }
  bool isAlphanumeric (char c)
  {
      return (c &gt;= 'A' &amp;&amp; c &lt;= 'Z')
             || (c &gt;= 'a' &amp;&amp; c &lt;= 'z')
             || (c &gt;= '0' &amp;&amp; c &lt;= '9');
  }
  std::string extractWord (const std::string&amp; fromMessage, int beginningAt)
  {
      string targetWord= "";
  
      targetWord = targetWord + fromMessage[beginningAt];
  
      return targetWord;
  }
  </code></pre>
  
  <p>edit: after trying to use targetWord as an array (which I couldn't define the size) and using several different for and while loops within extractWord I found a solution:</p>
  
  <pre><code>std::string extractWord (const std::string&amp; fromMessage, int beginningAt)
  {
    string targetWord= "";
  
  while (isAlphanumeric(fromMessage[beginningAt++]))
  {
      targetWord  = targetWord + fromMessage[beginningAt-1];
  }
  
  
  return targetWord;
  </code></pre>