C Program to concatenate two strings

What is the string concatenation?

Here in this c program, we need to concatenate two strings, which means we need to add the second string to the end of the first string. For example, 'hello' is the first string and 'world' is the second string. We need the output as "hello world" as a concatenated string.

How is string concatenation done in C?

To achieve this, declare two arrays with chat type, 'string1', and 'string2'. Then assign all the array locations to null using 'strset() function.' now accept two strings from the user and store it in 'string1' and 'string2'. Apply a for loop to traverse to the end of the first string. After reaching the end, we start the next loop for adding the second string to the first string. The result will be the concatenated string as 'string1'. Finally, display the concatenated string as string1.

What is strset()?

The strset() function is used to set all Characters of the String by a given Character. The 'string.h' Header file contains this function; the syntax of 'strset()' is:

strset(string1,char1);

The strset() function has mainly two Parameters.

  • The first parameter is a string,strng1.
  • The second parameter is a Character,char1.

Here the strset() Function will set string1's all characters by char1.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the character Array 'string1' and 'string2'.

STEP 3: Declare the integer variables i, j, pos.

STEP 4: Set all occurrences of the string1, string2 to NULL, and 'i=0'.

STEP 5: Read the string1 form the user ,using gets() function.

STEP 6: Read the string2 form the user ,using gets() function.

STEP 7: Then display the First String as string1 and the Second String as string2.

STEP 8: By using a for loop with the condition string1[i] != '\0' increment 'i' by one.For getting the end position of the First String.

STEP 9: Assign 'pos=i' and 'pos=i', j=0.

STEP 10: By using another for loop with the condition string2[j] != '\0' do step 11.

STEP 11: string1[i] = string2[j++].

STEP 12: Increment 'i' by 1 and repeat step 10.

STEP 13: Set the last character of 'string1' to NULL.

STEP 14: 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 string1*/
  pos = i;
  for (i = pos, j = 0; string2[j] != '\0'; i++) {
    string1[i] = string2[j++];
  }
  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