Concatenating Strings (combining multiple strings into one)
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: helloworldInput: str1 = “Beginner”, str2 = “World”
Output: BeginnerWorld
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 = "Beginner" ; string str2 = "ForBeginner" ; 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 = "Beginner" ; // Custom input string 2 String s2 = "forBeginner" ); 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 = "Beginner" ; // Custom input string 2 string s2 = "forBeginner" ; 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 = "Beginner" ; let str2 = "ForBeginner" ; // 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 |
w3wiki
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 = "Beginner" ; string s1 = "forBeginner" ; // 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[] = "Beginner" ; char s1[] = "forBeginner" ; // 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 = "Beginner" ; String s1 = "forBeginner" ; // 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 = "Beginner" s1 = "forBeginner" # 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 = "Beginner" ; string s1 = "forBeginner" ; // 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 = "Beginner" ; let s1 = "forBeginner" ; // Appending the strings s = s.concat(s1); console.log(s); } // Driver code concatenateStrings(); // This code is contributed by Abhinav Mahajan (abhinav_m22) |
w3wiki
Time Complexity: O(N)
Auxiliary Space: O(N)