Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Concatenation of two strings
3.
Concatenation by Standard Method
3.1.
Algorithm
3.2.
Implementation 
3.2.1.
Complexity Analysis 
4.
Concatenation by String Library Function
4.1.
Algorithm
4.2.
Implementation
4.2.1.
Complexity Analysis 
5.
Frequently Asked Questions
5.1.
What is the difference between strings in C and C++?
5.2.
How can I remove the leading spaces from a string?
5.3.
How can I copy just a portion of a string?
5.4.
How can I convert a number to a string?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C Program to concatenate two Strings

Author Aditya Singh
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 

#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;
}

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

#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;  
}

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

Frequently Asked Questions

What is the difference between strings in C and C++?

Strings in C are just char arrays that conclude with a NULL byte by convention. You can malloc the space for them in terms of dynamic memory management (including the extra byte). It is your responsibility to manage memory when altering strings: Strings (std::string) are objects in C++ that come with all of the related automatic memory management and control, making them much safer and easier to use, especially for beginners. 

How can I remove the leading spaces from a string?

The C programming language lacks a standard function for removing leading spaces from a string. It is, however, simple to create your function to accomplish this. To remove the leading spaces from a string, you may easily create a custom function that uses the rtrim() function with the standard C library function strrev().

How can I copy just a portion of a string?

To copy a string section into another string, use the strncpy() method from the standard C library. 

How can I convert a number to a string?

The standard C library includes various functions for converting numbers of any type (integers, longs, floats, and so on) to strings and back. itoa(), one of these functions, is used to show how an integer is transformed into a string.

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 Coding Ninjas Studio 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.

Please upvote our blog to help other ninjas grow.

Happy Learning!

Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass