25th
Jan

# C Program to Concatenate Two Strings

• 25th Jan, 2023

### Overview:

As we know that time is the most important concern in our life. To do any task easily, We have to use software to perform it in a simpler manner. Software is a set of programs with related documents. The Software Program or Program is a set of instructions to do a specific task. Here, our specific task is to Concatenate Two Strings. First, I want to explain the String and Concatenation of Strings. Furthermore, I would like to demonstrate how to concatenate two strings. After all, I will write the logic of a C program to reverse a string and explain it with output.

### What is a String?

String is a combination of characters that may be any digit from 0 to 9 and Alphabets from A to Z. The repetition of characters does not matter. For instance, a String Conax uses 5 characters i.e. C,o,n,e,and x. Strings are actually one-dimensional arrays of characters terminated by a < strong > null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.

### What is Concatenation of String?

Concatenation is the process of appending one string to the end of another string. You can concatenate strings by using the + operator to add them together to make a new string. For instance, If First String is Welcome and second is User then concatenation of these two strings is Welcome User. Concatenation < strong > appends the second string after the first string.

### Logic to Concatenate Two Strings

To concatenate two strings str1 and str2, you will copy all characters of str2 at the end of str1. Below is the step by step descriptive logic to concatenate two strings:

• Step 1: Input two strings from user. Store it in some variable say str1 and str2. Here we need to concatenate str2 to str1
• Step 2: Find length of str1 and store in some variable say i = length_of_str;.
• Step 3: Run a loop from 0 till end of str2 and copy each character to str1 from the ith index.

### C program to reverse a string

```/**
* C program to concatenate two strings
*/

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
int i, j;

/* Input two strings from user */
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);

/* Move till the end of str1 */
i=0;
while(str1[i] != '\0')
{
i++;
}

/* Copy str2 to str1 */
j = 0;
while(str2[j] != '\0')
{
str1[i] = str2[j];
i++;
j++;
}

// Make sure that str1 is NULL terminated
str1[i] = '\0';

printf("Concatenated string = %s", str1);

return 0;
}
```

Save this program with the filename and extension .c like filename.c. Then compile it on turbo C or another compiler.

The output of the Program:

Enter first string: Welcome

Enter second string: User

Concatenated string = WelcomeUser

### Conclusion:

The internal working of this program depends upon the length of the strings.

After the successful compilation of the program, a message is displayed on the screen as Enter first string:

User Enter Welcome

Then, again a message displayed Enter second string:

If second time user entered User

Then, the final output is Concatenated string is WelcomeUser.

Valid name is required.

Valid name is required.

Valid email id is required.

29th
Dec

29th
Aug

29th
Jan