Concatenation of two strings
Concatenation of two strings is a common operation in computer programming and refers to the process of combining two or more strings into a single string. The resulting string contains all the characters from the original strings in the order they were combined.
In simple terms, concatenation is a way of putting two or more things together. In the context of strings, it means taking two or more separate strings and joining them to create a longer string.
For example, if we have two strings, "hello" and "world", we can concatenate them to create a new string, "helloworld". This can be achieved using the concatenation operator, which is usually a plus sign (+). In many programming languages, concatenation can also be performed using a string function or method, such as "concat" or "join".
Here given code implementation process.
/*
C program for
Concatenation of two strings
*/
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char s1[] = "Good";
char s2[] = " Luck";
// Display given strings
printf(" s1 : %s",s1);
printf("\n s2 : %s",s2);
// Concat the two strings
strcat(s1,s2);
// Display result
printf("\n Result : %s",s1);
return 0;
}
Output
s1 : Good
s2 : Luck
Result : Good Luck
// Java program for
// Concatenation of two strings
public class Concatenation
{
public static void main(String[] args)
{
String s1 = "Good";
String s2 = " Luck";
// Display given strings
System.out.print(" s1 : " + s1);
System.out.print("\n s2 : " + s2);
// Concat the two strings
String result = s1 + s2;
// Display result
System.out.print("\n Result : " + result);
}
}
Output
s1 : Good
s2 : Luck
Result : Good Luck
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Concatenation of two strings
int main()
{
string s1 = "Good";
string s2 = " Luck";
// Display given strings
cout << " s1 : " << s1;
cout << "\n s2 : " << s2;
// Concat the two strings
string result = s1 + s2;
// Display result
cout << "\n Result : " << result;
return 0;
}
Output
s1 : Good
s2 : Luck
Result : Good Luck
package main
import "fmt"
// Go program for
// Concatenation of two strings
func main() {
var s1 string = "Good"
var s2 string = " Luck"
// Display given strings
fmt.Print(" s1 : ", s1)
fmt.Print("\n s2 : ", s2)
// Concat the two strings
var result string = s1 + s2
// Display result
fmt.Print("\n Result : ", result)
}
Output
s1 : Good
s2 : Luck
Result : Good Luck
// Include namespace system
using System;
// Csharp program for
// Concatenation of two strings
public class Concatenation
{
public static void Main(String[] args)
{
String s1 = "Good";
String s2 = " Luck";
// Display given strings
Console.Write(" s1 : " + s1);
Console.Write("\n s2 : " + s2);
// Concat the two strings
String result = s1 + s2;
// Display result
Console.Write("\n Result : " + result);
}
}
Output
s1 : Good
s2 : Luck
Result : Good Luck
<?php
// Php program for
// Concatenation of two strings
function main()
{
$s1 = "Good";
$s2 = " Luck";
// Display given strings
echo(" s1 : ".$s1);
echo("\n s2 : ".$s2);
// Concat the two strings
$result = $s1.$s2;
// Display result
echo("\n Result : ".$result);
}
main();
Output
s1 : Good
s2 : Luck
Result : Good Luck
// Node JS program for
// Concatenation of two strings
function main()
{
var s1 = "Good";
var s2 = " Luck";
// Display given strings
process.stdout.write(" s1 : " + s1);
process.stdout.write("\n s2 : " + s2);
// Concat the two strings
var result = s1 + s2;
// Display result
process.stdout.write("\n Result : " + result);
}
main();
Output
s1 : Good
s2 : Luck
Result : Good Luck
# Python 3 program for
# Concatenation of two strings
def main() :
s1 = "Good"
s2 = " Luck"
# Display given strings
print(" s1 : ", s1, end = "")
print("\n s2 : ", s2, end = "")
# Concat the two strings
result = s1 + s2
# Display result
print("\n Result : ", result, end = "")
if __name__ == "__main__": main()
Output
s1 : Good
s2 : Luck
Result : Good Luck
# Ruby program for
# Concatenation of two strings
def main()
s1 = "Good"
s2 = " Luck"
# Display given strings
print(" s1 : ", s1)
print("\n s2 : ", s2)
# Concat the two strings
result = s1 + s2
# Display result
print("\n Result : ", result)
end
main()
Output
s1 : Good
s2 : Luck
Result : Good Luck
// Scala program for
// Concatenation of two strings
object Main
{
def main(args: Array[String]): Unit = {
var s1: String = "Good";
var s2: String = " Luck";
// Display given strings
print(" s1 : " + s1);
print("\n s2 : " + s2);
// Concat the two strings
var result: String = s1 + s2;
// Display result
print("\n Result : " + result);
}
}
Output
s1 : Good
s2 : Luck
Result : Good Luck
// Swift 4 program for
// Concatenation of two strings
func main()
{
let s1: String = "Good";
let s2: String = " Luck";
// Display given strings
print(" s1 : ", s1, terminator: "");
print("\n s2 : ", s2, terminator: "");
// Concat the two strings
let result: String = s1 + s2;
// Display result
print("\n Result : ", result, terminator: "");
}
main();
Output
s1 : Good
s2 : Luck
Result : Good Luck
// Kotlin program for
// Concatenation of two strings
fun main(args: Array < String > ): Unit
{
val s1: String = "Good";
val s2: String = " Luck";
// Display given strings
print(" s1 : " + s1);
print("\n s2 : " + s2);
// Concat the two strings
val result: String = s1 + s2;
// Display result
print("\n Result : " + result);
}
Output
s1 : Good
s2 : Luck
Result : Good Luck
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment