Concatenation of Two Strings (original) (raw)
Last Updated : 31 Oct, 2024
String concatenation is the process of joining two strings end-to-end to form a single string.
**Examples
**Input: s1 = “Hello”, s2 = “World”
**Output: “HelloWorld”
**Explanation: Joining “Hello” and “World” results in “HelloWorld”.**Input: s1 = “Good”, s2 = “Morning”
**Output: “GoodMorning”
**Explanation: Joining “Good” and “Morning” results in “GoodMorning”
Using + Operator
Almost all languages support + operator to concatenate two strings. In C, we have a library function strcat() for the same purpose.
C++ `
#include #include
using namespace std;
int main() { string s1 = "Hello, "; string s2 = "World!";
// Concatenating the strings
string res = s1 + s2;
cout << res << endl;
return 0;
}
C
#include <stdio.h> #include <string.h>
int main() { char s1[50] = "Hello, "; char s2[] = "World!";
// Concatenating the strings
strcat(s1, s2);
printf("%s\n", s1);
return 0;
}
Java
public class GfG { public static void main(String[] args) { String s1 = "Hello, "; String s2 = "World!";
// Concatenating the strings
String res = s1 + s2;
System.out.println(res);
}
}
Python
s1 = "Hello, " s2 = "World!"
Concatenating the strings
res = s1 + s2
print(res)
C#
using System;
class Program { static void Main() { string s1 = "Hello, "; string s2 = "World!";
// Concatenating the strings
string res = s1 + s2;
Console.WriteLine(res);
}
}
JavaScript
let s1 = "Hello, "; let s2 = "World!";
// Concatenating the strings let res = s1 + s2;
console.log(res);
`
Write your Own Method
- Create an empty result string.
- Traverse through s1 and append all characters to result.
- Traverse through s2 and append all characters to result. C++ `
#include #include using namespace std;
string concat(string s1, string s2) { string res;
// Append s1 to res
for (char c : s1) {
res += c;
}
// Append s2 to res
for (char c : s2) {
res += c;
}
return res;
}
int main() { string s1 = "Hello, "; string s2 = "World!";
// Call the function to concatenate the strings
string res = concat(s1, s2);
cout << res << endl;
return 0;
}
C
#include <stdio.h> #include <string.h>
void concat(const char* s1, const char* s2, char* res) { // Append s1 to res while (*s1) { *res++ = *s1++; }
// Append s2 to res
while (*s2) {
*res++ = *s2++;
}
*res = '\0';
}
int main() { const char* s1 = "Hello, "; const char* s2 = "World!"; char res[100];
// Call the function to concatenate the strings
concat(s1, s2, res);
printf("%s\n", res);
return 0;
}
Java
import java.util.StringBuilder;
public class Main { public static String concat(String s1, String s2) { StringBuilder res = new StringBuilder();
// Append s1 to res
for (char c : s1.toCharArray()) {
res.append(c);
}
// Append s2 to res
for (char c : s2.toCharArray()) {
res.append(c);
}
return res.toString();
}
public static void main(String[] args) {
String s1 = "Hello, ";
String s2 = "World!";
// Call the function to concatenate the strings
String res = concat(s1, s2);
System.out.println(res);
}
}
Python
def concat(s1, s2): res = ''
# Append s1 to res
for c in s1:
res += c
# Append s2 to res
for c in s2:
res += c
return res
if name == 'main': s1 = 'Hello, ' s2 = 'World!'
# Call the function to concatenate the strings
res = concat(s1, s2)
print(res)
C#
using System;
class Program { static string Concat(string s1, string s2) { string res = "";
// Append s1 to res
foreach (char c in s1) {
res += c;
}
// Append s2 to res
foreach (char c in s2) {
res += c;
}
return res;
}
static void Main() {
string s1 = "Hello, ";
string s2 = "World!";
// Call the function to concatenate the strings
string res = Concat(s1, s2);
Console.WriteLine(res);
}
}
JavaScript
function concat(s1, s2) { let res = '';
// Append s1 to res
for (let c of s1) {
res += c;
}
// Append s2 to res
for (let c of s2) {
res += c;
}
return res;
}
let s1 = 'Hello, '; let s2 = 'World!'; let res = concat(s1, s2); console.log(res);
`
Time Complexity : O(m + n) where m and n are lengths of the two strings.