Strings: Concatenation

Dave Sinkula 0 Tallied Votes 129 Views Share

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

#include <stdio.h>

char *mystrcat(char *dst, const char *src)
{
   char *start = dst;
   while ( *dst )
   {
      ++dst; /* find the current end of the string */
   }
   while ( *dst++ = *src++ )
      ; /* copy from 'src' up to and including the terminating null character */
   return start;
}

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

/* my output
Hello world
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.