C Program to do concatenation of two strings using array


March 3, 2022, Learn eTutorial
1236

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is the concatenation of strings?

Concatenate two Strings means we need to join two words together by adding one word after the other. Concatenate using the C program is very simple to understand and also useful.

How is string concatenation implemented in C?

The logic of this program is to first read two strings as 'string1' and 'string2'. We have to concatenate the second string at the end of the first string. After reading the strings, we have to calculate the end of the 'string1', where we have to join the second string. To find out the ending position of 'string1', we have to traverse through 'string1' using for loop with the condition string[i] != '\0'.

After getting the end position 'pos' of 'string1', place the first character of the 'string2' to 'string1's end position using for loop. Set the last character of the 'string1' to null. Then display the concatenated string as 'string1'.

ALGORITHM

STEP 1: Include the Header files to use the built-in function in the C program.

STEP 2: Include the Header 'string.h' for handling an array of characters.

STEP 3: Declare two-character Arrays 'string1' and 'string2'.

STEP 4: Declare the Integer variables' I', 'j', 'pos'.

STEP 5: Set all the occurrences in 'string1' and 'string2' to NULL.

STEP 6: Accept the First String into 'string1' and the Second String into 'string2' by using the gets() function.

STEP 7: Display both the 'string1' and 'string2' using the printf function.

STEP 8: To Concatenate the second String to the First String, first traverse to the 'string1' using a for loop with the condition string[i] != '\0'.

STEP 9: After traversing to the 'string1', we get the end position of the 'string1' where we have to add the Second String.

STEP 10: Using another for loop, we place each character of the 'string2' to the end of 'string1'.

STEP 11: Place the Last Character of the 'string1' to '\0'.

STEP 12: Then display the Concatenated String as 'string1'.

C Source Code

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

void main()
{

  char string1[20], string2[20];
  int i, j, pos;
  printf("Enter the first string :");
  gets(string1);
  fflush(stdin);
  printf("Enter the second string:");
  gets(string2);
  printf("First string  = %s\n", string1);
  printf("Second string = %s\n", string2);
  /*To concatenate the second string to the 
end of the string traverse the first to its end and attach the second string*/
  for (i = 0; string1[i] != '\0'; i++) {} /*null statement: simply traversing the string 1*/
  pos = i;
  for (i = pos, j = 0; string2[j] != '\0'; i++) {
    string1[i] = string2[j++]; /* add the string two to end of string one  */
  }
  string1[i] = '\0'; /*set the last character of string1 to NULL*/
  printf("Concatenated string = %s\n", string1);
}
                                      

OUTPUT

Enter the first string: CD-
Enter the second string: ROM
First string  = CD-
Second string = ROM

Concatenated string = CD-ROM