How to use and Implement own strcat in C

strcat feature in C

The strcat function appends a copy of the string to the end of the destination string (including the terminating null character). The initial character of the source string overwrites the null character at the end of the destination string.

Note: If copying takes place between objects that overlap, the behavior is undefined.

Syntax strcat in C:

char *strcat(char * restrict s1,const char * restrict s2);

Parameters:

s1— pointer to the destination array.

s2— pointer to the source array

Return:

The strcat function returns the value of s1.

 

Let’s see an example code to understand the functionality of the strcat in C. In this C code, I am appending the string from an array “src” to the array “dest”.

#include <stdio.h>
#include <string.h> /* declares the strcat() function */
#define SIZE 40

int main()
{
    char dest[SIZE] = "I love ";
    char src[] = "Aticleworld.com";

    //Append src in dest
    strcat(dest, src);

    //print dest after appending src
    printf("%s\n\n",dest);

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

    return 0;
}

Output:

strcat in C

 

Some important points you must know before using the strcat:

  • You must include string.h header file before using the strcat function in C.
  • When we use strcat(), the size of the destination buffer must be large enough to store the resultant string otherwise the behavior of strcat would be undefined. Let’s see an example code to understand this point.
#include <stdio.h>
#include <string.h> /* declares the strcat() function */
#define SIZE 6

int main()
{
    char dest[SIZE] = "Hello";
    char src[] = "Aticleworld";

    //Append src in dest
    strcat(dest, src);

    //print dest after appending src
    printf("%s\n\n",dest);

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

    return 0;
}
  • When you will run the above C program, the behavior would be undefined. So it is the programmer’s responsibility to make sure that the size of the destination array is long enough to accommodate all the characters of the source string including the null character.
  • The destination string must not be a literal string. If you do then strcat() function will try to modify a string literal which is undefined behavior and may cause the program to crash. See the example,
strcat("hello", "there");
  • The strcat function shows undefined behavior if the memory regions pointed to by the source and destination pointers overlap. Let’s see the example code,
#include <string.h>
#include <stdio.h>

int main()
{
    char str[60] = "I am going from Delhi to Gorakhpur";

    printf( "Function:\tstrcat with overlap\n" );

    printf( "Orignal :\t%s\n",str);
    printf( "Source:\t\t%s\n", str + 5 );
    printf( "Destination:\t%s\n", str + 11 );

    strcat( str + 11, str + 15);

    printf( "Result:\t\t%s\n", str );

    return 0;
}
  • You must use the valid memory for the source and destination string and it is the programmer’s responsibility to verify the same.

 

How to write your own strcat in C?

Your compiler/standard library will likely have a very efficient and tailored implementation of the strcat() function. So if not require avoid to create own version of the strcat function.

We can easily implement the strcat() function in C programming. You need to find the trailing null character of the destination string then you need to append the character of the source string including the null character. Let’s create our own version of the strcat() function in C.

Note: Below function only to understand the working of strcat. There is a lot of scenarios that are not handled in this function. You can handle the scenario as your requirement and if possible then use the standard strcat function (library function).

char *my_strcat(char *s1, const char *s2)
{
    //Pointer should not null pointer
    if((s1 == NULL) && (s2 == NULL))
        return NULL;

    //Create copy of s1
    char *start = s1;

    //Find the end of the destination string
    while(*start != '\0')
    {
        start++;
    }
    //Now append the source string characters
    //until not get null character of s2
    while(*s2 != '\0')
    {
        *start++ = *s2++;
    }

    //Append null character in the last
    *start = '\0';
    return s1;
}

 

 

Write strcat() function in one line (tricky question):

With the help of recursion, you can write the strcat function in one line.

Note: It is only a tricky question for learning don’t use such type of hack in the real application might create a serious issue for your application. Let’s see the code,

void my_strcat(char *s1, const char *s2)
{
  (*s1)? my_strcat(++s1, s2): (*s1++ = *s2++)? my_strcat(s1, s2): 0 ;
}

 

Let’s create a small application to test our own strcat() function.

#include <stdio.h>

#define SIZE 40


void my_strcat(char *s1, const char *s2)
{
  (*s1)? my_strcat(++s1, s2): (*s1++ = *s2++)? my_strcat(s1, s2): 0 ;
}


int main()
{
    char dest[SIZE] = "Welcome to ";
    char src[] = "Aticleworld";

    //Append src in dest
    my_strcat(dest, src);

    //print dest after appending src
    printf("%s\n\n",dest);

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

    return 0;
}

Output:

Welcome to Aticleworld

Aticleworld

 

Recommended Articles for you: