Open In App

Concatenating Strings (combining multiple strings into one)

Last Updated : 17 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two strings str1 and str2, the task is to concatenate these two strings.

Given two strings str1 and str2, the task is to concatenate these two strings.

Examples:

Input: str1 = “hello”, str2 = “world”
Output: helloworld

Input: str1 = “Geeks”, str2 = “World”
Output: GeeksWorld

Approach 1: Using the “+” operator

The most traditional method of concatenating strings is by using the “+” operator.

Below is the implementation of the above method:

C++




#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    string str1 = "Geeks";
    string str2 = "ForGeeks";
    string result = str1 + str2;
    cout << result << endl;
    return 0;
}


Java




// Java program to Illustrate Working of concat() method
// with Strings
 
// Main class
class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // Custom input string 1
        String s1 = "Geeks";
 
        // Custom input string 2
        String s2 = "forGeeks");
 
        String result = s1 + s2;
 
        // Print and display combined string
        System.out.println(result);
    }
}


Python3




# Defining strings
str1 = "Hello "
str2 = "Geek"
 
# + Operator is used to combine strings
str3 = str1 + str2
print(str3)


C#




// C# program to Illustrate Working of concat() method
// with Strings
using System;
 
// Main class GFG
class GFG
{
      // Main driver method
    public static void Main(string[] args)
    {
          // Custom input string 1
        string s1 = "Geeks";
       
          // Custom input string 2
        string s2 = "forGeeks";
        string result = s1 + s2;
       
          // Print and display combined string
        Console.WriteLine(result);
    }
}


Javascript




// Define the main function
function main() {
    // Declare and initialize strings
    let str1 = "Geeks";
    let str2 = "ForGeeks";
     
    // Concatenate the two strings
    let result = str1 + str2;
     
    // Print the concatenated result
    console.log(result);
}
 
// Call the main function
main();
 
// This Code Is Contributed By Shubham Tiwari


Output

GeeksForGeeks







Time Complexity: O(N)
Auxiliary Space: O(N)

Approach 2: Using In-built methods

Every programming language offers a built-in method as well to concatenate strings, such as:

Programming Language

In-Built method to find length of string

C

strcat()

C++

append( )

Java

concat()

JavaScript

concat()

Python

join()

Below is the implementation of the above method:

C++




// C++ Program for string
// concatenation using append
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    string s = "Geeks";
    string s1 = "forGeeks";
 
    // Appending the string.
    s.append(s1);
 
    cout << s << endl;
    return 0;
}


C




// C program to concatenate two
// strings using strcat function
#include <stdio.h>
#include <string.h>
 
int main()
{
 
    char s[] = "Geeks";
    char s1[] = "forGeeks";
 
    // concatenating the string
    strcat(s, s1);
    printf(s);
    return 0;
}


Java




// Java program to Illustrate Working of concat() method
// with Strings
 
// Main class
class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // Custom input string 1
        String s = "Geeks";
        String s1 = "forGeeks";
 
        // Custom input string 2
        s = s.concat(s1);
 
        // Explicitly assigning result by
        // Combining(adding, concatenating strings)
        // using concat() method
 
        // Print and display combined string
        System.out.println(s);
    }
}


Python3




s = "Geeks"
s1 = "forGeeks"
 
# join() method is used to combine the strings
print("".join([s, s1]))
 
# join() method is used here to combine
# the string with a separator Space(" ")
# s = " ".join([s, s1])
 
# print(s)


C#




using System;
 
class Program {
    static void Main() {
        string s = "Geeks";
        string s1 = "forGeeks";
 
        // Concatenating the strings.
        s = string.Concat(s, s1);
 
        Console.WriteLine(s);
    }
}


Javascript




// JavaScript code for the above approach
 
// Function to concatenate strings using the 'concat' method
function concatenateStrings() {
    let s = "Geeks";
    let s1 = "forGeeks";
 
    // Appending the strings
    s = s.concat(s1);
 
    console.log(s);
}
 
// Driver code
concatenateStrings();
 
// This code is contributed by Abhinav Mahajan (abhinav_m22)


Output

GeeksforGeeks







Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads