Strings: Concatenation

Dave Sinkula 0 Tallied Votes 285 Views Share

How might I write an implementation in C of the standard library function strcat ? Here's how I might.

#include <stdio.h>

char *mystrcat(char *dst, const char *src)
{
   char *start = dst;
   while ( *dst )
   {
      ++dst;
   }
   do {
      *dst++ = *src;
   } while ( *src++ );
   return start;
}

int main (void)
{
   char text[12] = "Hello", more[] = " world";
   puts(text);
   puts(mystrcat(text, more));
   return 0;
}

/* my output
Hello
Hello world
*/