C program to concatenate Two strings

Concatenation refers to the linking of two strings in a series. 
Suppose there are two strings, 'string1' and 'string2'. Our task is to connect string2 at the postfix of string1.
In this section, we will see four methods to concatenate two strings
1. concatenation of two strings.
2. Concatenation of two strings using dynamic memory allocation.
3. Concatenation of two strings using string function strcat.

4. Concatenation of two strings which consist of more than one word in the string.

1. concatenation of two strings.

#include <stdio.h>
#include<string.h>
int main() 
{
    char string1[20], string2[10];
    int i,size1,size2;
  
    printf("Enter the string1");
    scanf("%s",string1);
    size1=strlen(string1);
    printf("Enter the string2");
    scanf("%s",string2);
    size2=strlen(string2);
    for(i=0;i<=size2;i++)
    {
        string1[size1+i]=string2[i];
    }
    printf("concatenation of two strings\n%s", string1);
    return 0;
}

Output:
Enter the string1
Pine
Enter the string2
apple
concatenation of two strings
Pineapple

Explanation: 

In the above program, two strings  string1 and string2 are declare and taken as input. 

The strlen function is used to calculate the length of both the strings and store the values in integer variables size1 and size2. 

How do we concatenate the string2 with string1? 
look at the piece of code inside the block of for loop. 
First paint- for-loop will run till i<=size2. All the characters of string2 should be stored next to the string1. 
Second paint - The first  character of string2 has to be stored next to the last character of string1. 
Look at the below statement,
string1[size1+i]=string2[i]
Let's take an example, 
Take size1=4 and size2=5
string1 is 'Pine'
string2  is 'apple'
size of string1 is 10
size1+size2+1=10
size of string2 is 6
size2+1=6

The iteration of the loop for string1 and string2 will start from the 0th character.
For string1= 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th.
For string2= 0th 1st 2nd 3rd 4th 5th.
The 0th character of string2 will be stored in the 4th character of string1.
The size1 is 4 and 4+0=4.
0th iteration, string1[4+0]=string2[0]. The first character of string2 will be stored next to the last character of string2.
Similarly,
1th iteration, string1[4+1]=string2[1].
2th iteration, string1[4+2]=string2[2].
.
.
4th iteration, string1[4+4]=string2[4].
5th iteration, string1[4+5]=string2[5].
In the last iteration, the NULL character is transferred to the last memory allocation of string1. 

2. Concatenation of two strings using dynamic memory allocation

#include <stdio.h>
#include<stdlib.h>
int main() 
{
    
   char *string1;
   char *string2;
   int size1, size2, i;

   printf("Enter the value of size1 and size2:\n");
   scanf("%d %d",&size1,&size2);
   string1=(char*)malloc(size1+size2+1);
   string2=(char*)malloc(size2+1);
   printf("Enter the string1\n");
   scanf("%s",string1);
   printf("Enter the string2");
   scanf("%s",string2);
   
   
   printf("\nstring1= %s", string1);
   
   printf("\nstring2= %s",string2);
   for(i=0;i<=size2;i++)
   {
       string1[size1+i]=string2[i];
   }
   printf("\nconcatenation of string1 and string2\n%s",string1);
    return 0;
}

Output:
Enter the value of size1 and size2:
4 5
Enter the string1
Pine
Enter the string2
apple
string1= Pine
string2= apple
concatenation of string1 and string2 
Pineapple

Explanation:

In the above program, two-character pointers, *string1 and *string2 are declared. These character pointers will be used to take the input for two strings.
Why did we use character pointers, and why not normal character array?
If we use the normal character array than, the size of the character array will be fixed.  If we want to input the string greater than the size of the character array than, we can not be able to take the input greater than the character array. Also, if the input string is smaller than the size of the character array than, the remaining memory allocated in the array will be wasted.
To avoid all those issues, we are using the character pointer variable.
We have declared the two integer variables size1 and size2. These variables will decide the size of the string.
Using the malloc function, we can allocate the memories for both the strings as per our requirements.
Now, see the memory allocation for string1.
String1=(char*) malloc(size1+size2+1) ;
The size of string1 has to be the addition of size1 and size2 because the second string will be added next to the string1. The +1 is for the NULL character. We have to allocate the extra one-byte memory for the NULL character. The last character in the string is by default null character.
Now, after allocating the memory for both the strings, the input is taken.
How do we concatenate the string2 with string1?
Look at the piece of code inside the block of for loop.
The rest of the program is the same as the first program.

c-string-concatenation
3. Concatenation of two strings using string function strcat.

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main() 
{
    
   char *string1;
   char *string2;
   int size1, size2, i;

   printf("Enter the value of size1 and size2:\n");
   scanf("%d %d",&size1,&size2);
   string1=(char*)malloc(size1+size2+1);
   string2=(char*)malloc(size2+1);
   printf("Enter the string1\n");
   scanf("%s",string1);
   printf("Enter the string2");
   scanf("%s",string2);
   
   printf("\nstring1= %s", string1);
   printf("\nstring2= %s",string2);
   
   strcat(string1,string2);
   printf("\nconcatenation of string1 and string2\n%s",string1);
    return 0;
}

Output:
Enter the value of size1 and size2:
4 5
Enter the string1
Pine
Enter the string2
apple
string1= Pine
string2= apple
concatenation of string1 and string2 
Pineapple


Explanation:

The whole program is the same as the first program except the logic used to concatenate the string.
In the first program, we used the logic inside the for-loop.
In this program, we will directly use the string function call as strcat.
strcat function is used to concatenate the two strings.
If you want a deep explanation of the strcat function, you can visit the link below:

string function in c

4. Concatenation of two strings which consist of more than one word in the string.

#include<stdio.h>
#include<string.h>
int main()
{
 char string1[20];
 char string2[10];

 printf("Enter the string1:");
 gets(string1);
 printf("Enter the string2:");
 gets(string2);
 printf("\nstring1= %s",string1);
 printf("\nstring2= %s",string2);
 strcat(string1,string2);
 printf("\nconcatenation of two strings:\n %s",string1);

 return 0;
} 
 
output:
 Enter the string1:
 Best of 
 Enter the string2:
 luck Pooja
 concatenation of two strings:
 Best of luck Pooja 

Explanation:

scanf function can consider space in the string as a NULL character that is why a sentence can not be taken as input in scanf.
We can use the gets function to take a sentence as input. The gets function does not consider space as a NULL character. It considers the space as space.