title: Understanding C String Concatenation
body: <p>For an assesment in C i have to take two files with strings within them, and a string to concatenate them together e.g:</p>
  
  <p>If file1 contains</p>
  
  <pre><code>Now is the
  
  time for all
  
  good men to
  
  come to the aid of
  
  the party.
  </code></pre>
  
  <p>and file2 contains:</p>
  
  <pre><code>alpha
  
  beta
  
  gamma
  </code></pre>
  
  <p>then the output from</p>
  
  <pre><code>scat XX file1 file2
  </code></pre>
  
  <p>(scat being the program name)</p>
  
  <p>should be</p>
  
  <pre><code>Now is theXXalpha
  
  time for allXXbeta
  
  good men toXXgamma
  
  come to the aid ofXX
  
  the party.XX
  </code></pre>
  
  <p>and the output from</p>
  
  <pre><code>scat XX file2 file1
  </code></pre>
  
  <p>should be</p>
  
  <pre><code>alphaXXNow is the
  
  betaXXtime for all
  
  gammaXXgood men to
  
  XXcome to the aid of
  
  XXthe party.
  </code></pre>
  
  <p>In order to understand this i've been trying to play around with string concatenation trying to manually concat strings together. </p>
  
  <p>My question is (A: how to i access individual lines of a file in c?) and B: if I manually input:</p>
  
  <pre><code>  char *str1 = "hello";
  
    char *str2 = "world";
  </code></pre>
  
  <p>how would i add these strings together without a predefined function. My thoughts initially were to use a for loop:</p>
  
  <pre><code> for(str1; *str1 != '\0'; str1++)
  
       if(*str1 == '\0')
  
         *str1++ = *str2++;
  </code></pre>
  
  <p>my only issue is wouldn't this cause a seg fault due to memory access?</p>
  
  <p>when i encounter a '\0' in a string..how can i extend this string? unless i just copy both strings into a new  char str3[200] ?</p>
  
  <p>(all of the above is to try and help me understand how strings and string concat works, any assistance in learning this would be appreciated.)</p>
  
  <p>Euden</p>