Program to Concatenate Two Strings in C Language

Program-to-Concatenate-Two-Strings-in-C-Language

Program Description:

Write a Program to Concatenate Two Strings in C programming language. The program should accept two strings from the user and Concatenate or append the second string at the end of the first string.

Expected Input and Output:

Input:

Enter the first string : All limitations

Enter the second string :  are self-imposed

Output:

String after Concatenation: All limitations are self-imposed

Note, that we also added an extra space at the start of the second string.

Program Prerequisites:

It is recommended to know the basics of Strings and Arrays in C Programming. Please go through the following articles to learn more about these topics.

We are going to look at three different methods to concatenate two string in C Language in this article, They are

  1. Iterative Method: Manually concatenating the second string to first string using Loops.
  2. User-Defined Function: Using a user-defined function(without strcat) to concatenate the string.
  3. strcat library function: Using the strcat library function.

📢 This Program is part of the C String Practice Programs series

Concatenate Two Strings in C Iterative Method (without using strcat):

In order to concatenate two strings, We need to first calculate the length of the first string, Then use a for loop to iterate over the second string and Append elements from the second string to the first string.

Here is the step-by-step explanation of the program.

Program to Concatenate two string Iterative Method Algorithm:

  1. Declare two strings str1(i.e first string) and str2(second string). The str1 should have enough capacity to hold the two strings after concatenation.
  2. Read the two strings from the user and update str1 and str2 respectively.
  3. Calculate the Length of the First String( str1): To calculate the length of the str1, We can use a strlen function or a loop to count the number of elements. We are using a for loop to calculate the string length. The str1Length variable contains the string str1 length.
  4. Iterate over the Second String( str2) and Append elements to str1: Start a For loop to iterate over the str2 and append characters of the str2 to first string str1. As we are appending to str1, The starring index will be str1Length.
    • FOR Loop – Start with i=0 and continue till str2[i] != '\0'.
    • At each iteration copy str2[i] to str1[str1Length] – i.e str1[str1Length] = str2[i];
    • Increment the str1Length variable.
  5. Finally, Add the Terminating NULL character ( \0) to str1 string.
  6. Display the result on the console.

Concatenate Two string Iterative Method Program:

Let’s convert the above explanation into the C Code.

Program Output:

Compile and Run the program.

concate-two-strings-iterative-method-in-c

As we can see from the above output, The program is properly appending the str2 to str1.

Concatenate Two Strings in C using a user-defined function (custom strcat function):

Let’s look at another method to concatenate two strings in C language. Which is using a user defined function to concatenate two strings.

In the following program, We defined a custom strcat function named myStrConcate() function. The myStrConcate function takes two character pointers.

char * myStrConcate(char * dest, char * src);

The myStrConcate function concatenates the src string at the end of the dest string. This function also returns the dest string back to the caller.

Here is the program to concatenate two strings using custom strcat function

Program Output:

Compile and Run the program. We are using GCC compiler.

concat-two-strings-without-strcat-function

We can further divide the myStrConcatefunction into two functions. One function to calculate the length of the dest(i.e str1) string and other function to do the concatenate operation.

Program to Concatenate Two Strings in C using strcat library function:

Now let’s look at the third method to concatenate two strings in the C language, Which is using the strcat library function.

The C Standard library provides the strcat() function to concatenate strings. The strcat() function is declared in the string.h header file.

📢 Related: strcat function in c

Here is the syntax of the strcat function

char * strcat(char * dest, const char * src);

So Let’s rewrite the program and use the strcat library function to concatenate two strings.

Don’t forget to include the string.h header file.

📢 It is recommended to use the strcat library function to concatenate two strings.

Program Output:

Compile the program

$ gcc concat-strcat.c

The above command compiles the .c file and generates the executable file. by default, gcc compiler generates the executable file with a.out name. You can change this by passing the -o option to the gcc command.

Run the executable file using the ./.aout command.

concatenate-two-strings-using-strcat-function-in-c

As we can see from the above output, The first string is No Pressure, and the second string is No diamonds. The strcat() function concatenated two strings and generated the resultant string as No Pressure, No diamonds

Related C Practice Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Concatenate Two Strings […]

Leave a Reply