Javatpoint Logo
Javatpoint Logo

Strings Concatenation in C

The concatenation of strings is a process of combining two strings to form a single string. If there are two strings, then the second string is added at the end of the first string.

For example, Hello + javaTpoint = Hello javaTpoint

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

Analysis of the above program

  • In the above program, we declare two strings, i.e., first_string and second_string. 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 second_string is encountered. On each iteration, the character located at the jth position in the second_string gets stored in the ith position of the first_string. In this way, both the strings are combined together to form a single string.

Output

Strings Concatenation in C

Concatenate two strings using pointer

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 jth location in the string2 is appended to the string1 by using the statement *str1=*str2.

Output

Strings Concatenation in C

Concatenation of two strings using strcat()

Now we will see how to concatenate two strings using strcat() function. The strcat() is an inbuilt function defined in a string.h header file.

Let's look at the example.

In the above code, we use the strcat() function to concatenate two strings. It takes two strings as a parameter and then appends the second string at the end of the first string.

Output

Strings Concatenation in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA