title: C++ primer 5th Ed: The Word Transformation Program
body: <p>On chapter 11 from C++ Primer 5 Edition, the title "The Word Transformation Program" it has a function called <code>word_transform()</code> which is defined this way:</p>
  
  <pre><code>void word_transform(ifstream &amp;map_file, ifstream &amp;input)
  {
      auto trans_map = buildMap(map_file); // store the transformations
      string text; // hold each line from the input
      while (getline(input, text)) { // read a line of input
          istringstream stream(text); // read each word
          string word;
          bool firstword = true; // controls whether a space is
          printed
              while (stream &gt;&gt; word) {
                  if (firstword)
                      firstword = false;
                  else
                      cout &lt;&lt; " "; // print a space between words
                                   // transform returns its first argument or its transformation
                  cout &lt;&lt; transform(word, trans_map); // print the output
              }
          cout &lt;&lt; endl; // done with this line of input
      }
  }
  </code></pre>
  
  <ul>
  <li><p>What I see if I am not wrong that the Boolean object <code>firstword</code> is redundant here and there's no need to check for a space and instead simply after calling <code>transform_word</code> which returns either a transformed text or original one then print it and simply print a space after it:</p>
  
  <pre><code>void word_transform(ifstream &amp;map_file, ifstream &amp;input)
  {
      auto trans_map = buildMap(map_file); // store the transformations
      string text; // hold each line from the input
      while (getline(input, text)) { // read a line of input
          istringstream stream(text); // read each word
          string word;
  
          while (stream &gt;&gt; word)
              cout &lt;&lt; transform(word, trans_map) &lt;&lt; " ";
          cout &lt;&lt; endl; // done with this line of input
      }
  }
  </code></pre></li>
  <li><p>As you can see the function works fine. Please help whether I am correct or use the book's function.</p></li>
  </ul>