title: C: What is the best and fastest way to concatenate strings
body: <p>I currently concatenate strings in c using the <code>strcat()</code> function from <code>string.h</code> library.</p>
  <p>I thought about it, and I got to a conclusion that it should be very expensive function, as before it starts to concatenate, it has to iterate over the char array until it finds the <code>'\0'</code> char.</p>
  <p>For example, if I concatenate the string <code>&quot;horses&quot;</code> 1000 times using <code>strcat()</code>, I'll have to pay
  <code>(1 + 2 + 3 + ... + 1000) * strlen(&quot;horses&quot;) = (1000*1001)/2 * 6 = 3003000</code></p>
  <p>I thought about the non-standard way, of maintaining an integer with the string length, and then sending to <code>strcat()</code> the pointer to the end of the string:</p>
  <pre><code>strcat(dest + dest_len, &quot;string&quot;);
  </code></pre>
  <p>In this case, I'll pay only <code>1000 * strlen(&quot;horses&quot;) = 1000 * 6 = 6000</code>.</p>
  <p><code>6000</code> is 500x smaller than <code>3003000</code>, so it can be very critical for performance if you make a lot of such concatenations.</p>
  <p>Is there some more standard way to do it, looking better than my solution?</p>