strcat( ) in C: A Comprehensive Guide

strcat( ) in C: A Comprehensive Guide

Learn how to use the built-in strcat( ) function, as well as how to build your own custom strcat( ) function from scratch.

Play this article

Introduction

The strcat( ) function is one of the string manipulation in C Programming that combines two strings, that is, it takes the second string and appends it to the first string. This function is contained in the string.h header file.

The Built-in strcat( ) Function

This function is one of the major six built-in string functions contained in the string.h header file that concatenates two strings: This simply means it joins two strings to one.

To use this function, we need to pass in two arguments, the first one being the destination, which is the string to appear first, while the second is the source that will join the destination, which is the first string.

This is an example of using the strcat ( ) function:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char dest[20] = "I Love ";
    char src[20] = "Software Engineering";

    printf("Before strcat() = %s\n", dest);

    strcat(dest, src);

    printf("After strcat() = %s\n", dest);

    return (0);
}

One very important thing here is to set the size of the array of the variable carrying the first string, that is the string that is going to be concatenated to and set the size to be large enough to contain all the string of the second variable.

This is how to apply and use the inbuilt strcat( ) string function. Always remember to include the string.h header file at the beginning of the file as that is the file that contains the function declaration.

Building a Custom strcat( ) Function

To build a custom strcat( ) function, here we will be using the name _strcat( ) for the custom function.

The logic behind this function is that it accepts two arguments, with the first argument being the dest and the second argument the src.

The second argument is appended to the first argument, for this to happen, the null character that terminates the first string is removed, then the string from the second argument is appended to the end of the first argument string and then finally, a null character is appended at the end of the concatenated string to terminate the concatenated string.

And another thing is that the first argument which will be an array of strings should contain a size that is big enough to contain the string that will be concatenated to it.

Here is the algorithm that we are going to implement to build our custom strcat( ) function:

  1. Loop through each character of the string stored in the first argument until it gets to the null terminating character, to get the length of the string in the first argument.

  2. Loop through each character of the second string, and as you loop through, set the value of the first argument starting from the length of the first argument, setting it equal to the value of the second string whose looping should start from 0 till it hits the null terminating character.

  3. Immediately after the loop, append a null terminating character to mark the end of the string.

  4. Finally, return the first argument as it now contains the new string that is a result of concatenation.

Below is the implementation of this algorithm:

#include <string.h>
char *_strcat(char *dest, char *src)
{
    int i, j, len = 0;

    for (i = 0; dest[i] != '\0'; i++)
    {
        len++;
    }

    for (j = 0; src[j] != '\0'; j++)
    {
        dest[len + j] = src[j];
    }
    dest[len + j] = '\0';

    return (dest);
}

To test the strcat ( ) function we just built, let us look at the example below:

#include <stdio.h>

char *_strcat(char *dest, char *src);
int main()
{
    char dest[30] = "I Love ";
    char *src;
    src = "Software Engineering";

    printf("Before the strcpy = %s\n", dest);
    _strcat(dest, src);
    printf("After the strcpy = %s\n", dest);

    return (0);
}

char *_strcat(char *dest, char *src)
{
    int i, j, len = 0;

    for (i = 0; dest[i] != '\0'; i++)
    {
        len++;
    }
    for (j = 0; src[j] != '\0'; j++)
    {
        dest[len + j] = src[j];
    }
    dest[len + j] = '\0';
    return (dest);
}

This is how to build your strcat function, known as a string concatenate.

Conclusion

This is how you can build your strcat (string concatenate) string function from scratch and implement it.

Thank you for reading, you can connect with me on Twitter and LinkedIn.