Strings Concatenation In C

We can concatenate the strings in the following three ways:

  • Concatenate two strings using loop
  • Concatenate two strings using pointer
  • Concatenate two strings using strcat function
  • Concatenate two strings using loop

    Example
    
    #include <stdio.h>
    int main()
    {
        char first_string[20]; // declaration of char array variable
        char second_string[20]; // declaration of char array variable
        int i;  // integer variable declaration
        printf("Enter the first string");
        scanf("%s",first_string);
        printf("\nEnter the second string");
        scanf("%s",second_string);
        for(i=0;first_string[i]!='\0';i++); 
        
        
        for(int j=0;second_string[j]!='\0';j++)
        {
          
            first_string[i]=second_string[j];
            i++;
        }
        first_string[i]='\0';
       printf("After concatenation, the string would look like: %s", first_string);
    return 0;
    

Analysis of the above program

  • In the above program, we declare two strings, i.e., firststring and secondstring . The values of these two strings are taken by the user as an input.
  • We will concatenate these two strings and store the result of concatenation in the first_string
  • We declare a new integer variable named ' i '. After declaration, we will run a loop that iterates from i=0 till the null character of the first_string is encountered. When the execution of the loop is completed, then the value of ' i ' becomes equal to the length of the string plus one.
  • We will define another new loop, which is basically used to concatenate the two strings. This loop starts from j=0 and executes until the null character of the secondstring is encountered. On each iteration, the character located at the j th position in the secondstring gets stored in the i t h position of the first_string . In this way, both the strings are combined together to form a single string.

Output

Output

Enter the first string
Enter the second stringAfter concatenation, the string would look like: [value]

Concatenate two strings using pointer

Example

#include <stdio.h>
int main()
{
  char string1[20]; // char array declaration
  char string2[20]; // char array declaration
  int i=0,j=0; // declaration of integer variables
  char *str1; // declaration of char pointer
  char *str2;  // declaration of char pointer
  str1=string1;
  str2=string2;
  printf("Enter the first string");
  scanf("%s",string1);
  printf("\nEnter the second string");
  scanf("%s", string2);
  while(string1[i]!='\0')
  {
      
     ++str1;
     i++;
  }
  while(string2[j]!='\0')
  {
      *str1=*str2;
      str1++;
      str2++;
      j++;
  }
  printf("The concatenated string is %s",string1);

    return 0;
}

Analysis of the above program

  • In the above program, we have declared character array variables, i.e., string1 and string2 . We take the two strings as user input and store them in the variables string1 and string2,
  • We declare two char pointers str1 and str2. The str1 contains the address of string1, and str2 contains the address of string2.
  • We create a loop which iterates until the null character of string1 is encountered. When the execution of the loop is completed, then the pointer str1 points to the location that exists just after the location of the last character.
  • We define another while loop that iterates till the null character of string2 is encountered. On each iteration, the character stored at the j th location in the string2 is appended to the string1 by using the statement str1=str2.

Output

Output

Enter the first string
Enter the second stringAfter concatenation, the string would look like: [value]

Concatenation of two strings using strcat

Now, let's explore the process of combining two strings by utilizing the strcat function. This function is a built-in function that is specified in the string.h header file.

Let's look at the example.

Example

#include <stdio.h>
#include<string.h>
int main()
{
  char s1[20]; // declaration of char array
  char s2[20]; // declaration of char array
printf("Enter the first string : ");
scanf("%s", s1);
printf("\nEnter the second string :");
scanf("%s",s2);
strcat(s1,s2);
printf("The concatenated string is : %s",s1);
    return 0;
}

In the provided code snippet, the strcat function is employed to combine two strings together. This function requires two strings as arguments and then adds the content of the second string to the end of the first string.

Output

Input Required

This code uses input(). Please provide values below: