String Concatenation in C

We will write a Program for string concatenation in C language. If First string is “Programming is awesome, ” and the second string is “I love it.” then after concatenation first string will be “Programming is awesome, I love it.

Concatenate two strings without using string manipulation library functions

We can write a C program to concatenate two string without using strcat() function.

#include<stdio.h>
int main()
{
    char str1[100], str2[50];
    int i, j;

    printf("Enter first string: ");
    fgets(str1, sizeof(str1), stdin);

    printf("Enter second string: ");
    fgets(str2,sizeof(str2),stdin);

    // calculate the length of string str1 
    // and store it in the variable i

    for(i=0; str1[i]!='\0'; i++);
    i--;
    str1[i] = ' ';
    i++;

    for(j=0; str2[j] != '\0'; j++, i++)
    {
       str1[i] = str2[j];
    }
    str1[i] = '\0';

    printf("After Concatenation first string becomes:\n");
    puts(str1);

    return 0;
}

Output:-

Enter first string: Welcome to the programming world
Enter second string: Learn C programming
After Concatenation first string becomes:
Welcome to the programming world Learn C programming

In the above program, we take two strings as input from the user. The length of the first string is calculated using for loop. The function fgets() also adds a new line entered from the keyboard to the string. So, to remove this new line with space we write i–; str1[i]=’ ‘; and i++; before the for a loop. Now the variable i the index is at the end of the string str1, so str2 characters will be filled from the index i. At last to the string as a valid string, we place a null character ‘\0’ at the end of the concatenated string.

C program to concatenate two string using strcat()

For string concatenation in C language, we can also use strcat() function defined in “string.h”. The strcat() function concatenated (join) two strings. It required header file <string.h>

In string concatenate functions append one string to the end of the other string. The size of the destination string array is assumed to be large enough to hold the resulting string. If it isn’t, the data at the end of the string array are destroyed.

The function declaration:-

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses
char *strcat(char* str1,
             const char* str2);

The function copies str2 to the end of str1, beginning with str1’s delimiter (null character). That is, the delimiter (null character) is replaced with the first character of str2. The delimiter from str2 is copied to the resulting string to ensure that a valid string results. The length of the resulting is the sum of the length of str1 plus the length of str2.

String concatenation in c
#include<stdio.h>
#include<string.h>
int main()
{
   char str1[50];
   char str2[50];

   printf("Enter first string: ");
   gets(str1);
   printf("Enter second string: ");
   gets(str2);

   strcat(str1,str2);
   printf("Result string is: %s",str1);

   return 0;
}

Output:-

Enter first string: Learn C programming with
Enter second string: Know Program
Learn C programming with Computer Corner
Know Program

The strcat() doesn’t give space between two string so, to add a space we need to give space at the last of string1 or the beginning of string2.

Concatenate two string using strncat()

The strncat() also concatenate the string. The function declaration for length-controlled string concatenate function, strncat() is:-

char *strcat(char* str1,
             const char* str2, 
             int size);

If the length of string2 is less than size, then all call works the same as the basic string concatenation function strcat(). However, if the length of string2 is greater than size, then only the number of characters specified by size is copied, and a null character is appended at the end.

#include<stdio.h>
#include<string.h>
int main()
{
   char str1[50]="Know";
   char str2[50]="Program";

   strncat(str1,str2,3);
   printf("Result string is: %s",str1);

   return 0;
}

Output:-

Result string is: KnowPro

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *