/***********************************************************************
 * strcat.c
 *
 * This program mimics the C library's strcat() function, concatenating
 * a source string to the end of a destination string.
 ***********************************************************************/

#include <stdio.h>

/* str1 is the destination string.  The char array which holds it
 * is declared large enough to hold it and the source string.
 * str2 is the source string.
 */

char str1[50] = "My brain hurts,";
char str2[] = " but I feel much better now.";

int main()
{
   char *dest = str1;   /* Pointer into the destination string. */
   char *src = str2;    /* Pointer into the source string. */

   /* Find the end of the destination string. */

   while (*dest != '\0')
      dest++;

   /* Start concatenating the source string onto the end of the
    * destination string.
    */

   while (*src != '\0')
   {
      *dest = *src;
      dest++;
      src++;
   }

   /* NULL-terminate the concatenated string and print it. */

   *dest = '\0';

   printf("%s\n", str1);

   return 0;
}
