Today we will talk something about string manipulation, defined in string.h header file. We use header file when we want to use a function containing in string library functions. Among them, strcat function is one & very important.

We know that we can’t join two strings together by simple arithmetic addition. It means that the statements given below aren’t valid.

string = str1 + str2;<br>string = str3 + "ungratified coder";

But sometime we need to concatenate two strings together. In those situations, what will we do? Obviously we will use strcat function in those case. strcat function concatenates two strings. Its basic form is given below:

**strcat(str1, str2);**

Here str1 & str2 are two parameters of this function. Now consider the following program:

#include <stdio.h>
#include <string.h>
 
int main()
{
    char str1[100] = "He is ";
    char str2[20] = "Ungratified";
    strcat(str1, str2);
    puts(str1);
    strcat(str1, " Coder.");
    puts(str1);
    return 0;
}

Output:

He is Ungratified

He is Ungratified Coder.

Now let me explain what actually happens in this program.

strcat(str1, str2);

When we use strcat function for the concatenation of two strings str1 & str2, it appends str2 to str1 by removing the null character at the end of str1 and placing str2 from here. If we want to append string constant (” Coder.“) to str1, then we have to write the statement given below:

strcat(str1, " Coder.");

We can use the strcat function in nested form. C permits such kind of concatenation. Please consider the following program:

#include <stdio.h>
#include <string.h>
 
int main()
{
    char str1[100] = "He is ";
    char str2[20] = "Ungratified";
    strcat(strcat(str1, str2), " Coder.");
    puts(str1);
    return 0;
}

Output:

He is Ungratified Coder.

Here all the three strings have been concatenated. The result of this concatenation has been stored in str1.

I think you have understood the uses of strcat function. Now just practice this function. Happy Coding!!!