Table of contents
1.
Introduction
2.
Concatenation of two strings
3.
Concatenation by Standard Method
3.1.
Algorithm
3.2.
Implementation 
3.3.
C
3.3.1.
Complexity Analysis 
4.
Concatenation by String Library Function
4.1.
Algorithm
4.2.
Implementation
4.3.
C
4.3.1.
Complexity Analysis 
5.
Concatenate Two Strings Using Loop
5.1.
Algorithm
5.2.
Implementation
5.3.
C
5.3.1.
Complexity Analysis
6.
Concatenate Two Strings Using Pointer
6.1.
Algorithm
6.2.
Implementation
6.3.
C
6.3.1.
Complexity Analysis
7.
Concatenate Two Strings Using Strcat() Function
7.1.
Algorithm
7.2.
Implementation
7.3.
C
7.3.1.
Complexity Analysis
8.
Concatenate Two Strings without Using Strcat()
8.1.
Algorithm
8.2.
Implementation
8.3.
C
8.3.1.
Complexity Analysis
9.
Frequently Asked Questions
9.1.
How to print two strings in C?
9.2.
How to compare two strings in C?
9.3.
How to concatenate string pointers in C?
10.
Conclusion
Last Updated: Sep 15, 2024
Easy

C Program to concatenate two Strings

Author Aditya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

string concatenation in c

A string is a sequence of characters that ends with a null character ( \0 ) in C programming.

For Example:

char c[ ] = "Hello strings";

By default, the compiler appends a null character \0 to the end of a sequence of characters wrapped in double quotation marks.

H e l l o   s t r i n g s \0

Memory diagram

Various Operations on a string in C programming:

  • Making copies of the string
  • Comparison of two strings
  • Concatenation of two strings
  • Finding substring from a given string
  • String length finding 

In this article, we are going to look at how to concatenate two strings in C language. 

So, let’s get started and try it on an online C compiler.

Also check this Starcat() in C and  Tribonacci Series

Concatenation of two strings

In programming, concatenation refers to the process of combining two strings.

Concatenation is a phrase that means "to join two things together."

It simply means joining two strings by the standard method (using programming logic)

Or by using the string library function that is strcat() function.

For Example

Input:

Output:

Also See, Binary to Hex Converter and C Static Function.

Concatenation by Standard Method

Algorithm

  1. The two strings will be combined into one.
  2. Using the gets() function, read two strings entered as gets(s1) and gets(s2).
  3. Using the string library function strlen(s1), get the length of the string s1 and set it to j.
  4. The for loop iterates across the for(i=0;s2[i]!='\0′;i++) structure. Append the characters of string s2 to the string s1[i+j] until there are no more characters in the string s2. The string s2 is appended to the end of the string s1.
  5. Print the s1 string, which has been concatenated.

Implementation 

  • C

C

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

int main()
{
char string1[1000], string2[1000]; 
int i,j;
printf("Enter  string number 1: ");
gets(string1);
printf("Enter  string number 2: ");
gets(string2);
j=strlen(string1);
  
for(i=0;string2[i]!='\0';i++) 
{
   string1[i+j]=string2[i];
}
string1[i+j]='\0';
printf("combined two strings ='%s'\n",string1);
  
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Output

Complexity Analysis 

Time Complexity: O(n), where n is the length of the second string. 

You can also read about dynamic array in c and Short int in C Programming

Concatenation by String Library Function

Algorithm

  1. Here strcat(s1,s2) is a string library function found in the "string.h" header file.
  2. The strcat(s1,s2) function joins the strings s2 and s1.

Parameters 

Description

s1

It is the destination string to which the s2 string is to be attached.

s2

It is the source string that is going to attach to the destination one i.e s1.

 

It works as strcat() function of string concatenation is called here which takes two arguments or say parameters first are the destination and the second one is the source like the destination is the string after which the source string has to be attached.

Implementation

  • C

C

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


int main()
{
char string1[1000],string2[1000]; 
printf("Enter  string1: ");
gets(string1);
printf("Enter  string2: ");
gets(string2);
strcat(string1,string2);
printf("combined two strings ='%s'\n",string1);

return 0; 
}
You can also try this code with Online C Compiler
Run Code

Output

Output

Complexity Analysis 

Time Complexity: O(l1+l2), where l1 and l2 are the lengths of the first and second strings respectively.  

You can practice by yourself with the help of online c compiler.

Must Read what is storage class in c

Concatenate Two Strings Using Loop

Algorithm

  1. Declare two character arrays, str1 and str2.
     
  2. Initialize i and j as loop variables.
     
  3. Move the i pointer to the end of str1 by iterating until you find the null terminator (\0).
     
  4. Set j to 0 and start appending characters from str2 to str1 using the index i.
     
  5. Continue copying characters from str2 to str1 until you encounter the null character in str2.
     
  6. Append a null terminator (\0) at the end of str1 to mark the end of the string.

Implementation

  • C

C

#include <stdio.h>

void concatenate(char str1[], char str2[]) {
int i = 0, j = 0;

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

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

// Add null character at the end
str1[i] = '\0';
}

int main() {
char str1[100] = "Hello, ";
char str2[] = "World!";

concatenate(str1, str2);

printf("Concatenated String: %s\n", str1);
return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Concatenated String: Hello, World!

Complexity Analysis

The time complexity of concatenating two strings using a loop is O(n + m), where n is the length of the first string and m is the length of the second string.

Concatenate Two Strings Using Pointer

Algorithm

  1. Declare two character pointers str1 and str2.
     
  2. Move the str1 pointer to the null terminator (\0) by incrementing it until *str1 becomes 0.
     
  3. Use a loop to copy the characters from str2 to the position pointed to by str1.
     
  4. After copying all characters, append the null terminator (\0) to str1.

Implementation

  • C

C

#include <stdio.h>

void concatenate(char *str1, char *str2) {
// Move the pointer to the end of str1
while (*str1) {
str1++;
}

// Append str2 to str1
while (*str2) {
*str1 = *str2;
str1++;
str2++;
}

// Add null terminator at the end
*str1 = '\0';
}

int main() {
char str1[100] = "Hello, ";
char str2[] = "World!";

concatenate(str1, str2);

printf("Concatenated String: %s\n", str1);
return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Concatenated String: Hello, World!

Complexity Analysis

The time complexity of concatenating two strings using pointers is also O(n + m).

Concatenate Two Strings Using Strcat() Function

Algorithm

  1. Declare two character arrays, str1 and str2.
     
  2. Use the strcat(str1, str2) function to concatenate str2 at the end of str1.
     
  3. The function automatically moves to the end of str1 and appends str2.
     
  4. The null terminator (\0) is automatically handled by strcat().

Implementation

  • C

C

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

int main() {
char str1[100] = "Hello, ";
char str2[] = "World!";

// Concatenate using strcat
strcat(str1, str2);

printf("Concatenated String: %s\n", str1);
return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Concatenated String: Hello, World!

Complexity Analysis

The time complexity is O(n + m), where n and m are the lengths of the two strings.

Concatenate Two Strings without Using Strcat()

Algorithm

  1. Declare two character arrays, str1 and str2.
     
  2. Initialize index i and j.
     
  3. Move the i pointer to the end of str1.
     
  4. Use a loop to copy each character from str2 to the end of str1.
     
  5. Continue copying until the null terminator of str2 is reached.
     
  6. Add the null terminator (\0) at the end of the concatenated string.

Implementation

  • C

C

#include <stdio.h>

void concatenate(char str1[], char str2[]) {
int i = 0, j = 0;

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

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

// Add null terminator at the end
str1[i] = '\0';
}

int main() {
char str1[100] = "Hello, ";
char str2[] = "World!";

concatenate(str1, str2);

printf("Concatenated String: %s\n", str1);
return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Concatenated String: Hello, World!

Complexity Analysis

When manually concatenating two strings without using strcat(), the time complexity remains O(n + m).

Frequently Asked Questions

How to print two strings in C?

You can use the printf() function to print two strings by passing both strings as arguments. For example: printf("%s %s", str1, str2);.

How to compare two strings in C?

You can use the strcmp() function to compare two strings. It returns 0 if the strings are equal, a positive value if the first is greater, and a negative value if the second is greater.

How to concatenate string pointers in C?

To concatenate string pointers in C, move the first pointer to its null terminator, then copy characters from the second string using a loop or strcat() function.

Conclusion

In this article, we see the implementation of the C program to concatenate two strings by various methods. We had also seen the output of the written program on some random input.

If you want to learn more about C programs, visit the given links below:

Transpose of a Matrix in C.

Bubble Sort Program in C

Reverse A String In C

Getch in C

C Program to count the length of string without using any library function

Data Structure

Ternary Operator in C

Conditional Operator in C
 

Recommended problems -

Please refer to our guided paths on Code360 to learn more about DSA, Competitive Programming, Javascript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Live masterclass