title: Single quote issues with C++ find and replace function
body: <p>Here is my code for finding a sequence in a string and replacing it with another:</p>
  
  <pre><code>std::string find_and_replace( string &amp;source, string find, string replace )
  {
      size_t j;
      for ( ; (j = source.find( find )) != string::npos ; )
      {
          source.replace( j, find.length(), replace );
      }
      return source;
  }
  </code></pre>
  
  <p>Everything works fine when I call something like:</p>
  
  <pre><code>find_and_replace(test, "foo", "bar")
  </code></pre>
  
  <p>My application requires me to replace a single quote with two single quotes, not a double quote. For example I would call:</p>
  
  <pre><code>find_and_replace(test, "'", "''")
  </code></pre>
  
  <p>But whenever I call this, the function freezes up for some reason. Does anyone know what might be the cause of this problem?</p>
  
  <p>Edit: based on the answers I've gotten, I have fixed the code:</p>
  
  <pre><code>std::string find_and_replace( string &amp;source, string find, string replace )
  {
      string::size_type pos = 0;
      while ( (pos = source.find(find, pos)) != string::npos ) {
          source.replace( pos, find.size(), replace );
          pos += replace.size();
      }
      return source;
  }
  </code></pre>
  
  <p>I hope this helps some people having the same problem.</p>