Concatenate Two Strings in C, C++, Java and Python

Select Articles

Concatenate Two Strings in C, C++, Java and Python

Program to concatenate two strings is discussed here. Two strings are obtained as input and the resultant string should be the concatenations of the two strings.

For example, String 1 = "Hello" and String 2 = "World"

The output string will be "HelloWorld."

 

C

// C program to concatenate two strings

#include

int main()

{

char str1[100], str2[100], i, j;

printf(“\nEnter first string: “);

scanf(“%s”, str1);

printf(“\nEnter second string: “);

scanf(“%s”, str2);

for(i = 0; str1[i] != ‘\0’; ++i);

for(j = 0; str2[j] != ‘\0’; ++j, ++i)

{

str1[i] = str2[j];

}

str1[i] = ‘\0’;

printf(“Resultant string : %s”, str1);

return 0;

}

 

C++

// C++ program to concatenate two strings

#include

using namespace std;

int main()

{

char str1[100], str2[100], i, j;

cout << “\nEnter first string: “;

cin >> str1;

cout << “\nEnter second string: “;

cin >> str2;

for(i = 0; str1[i] != ‘\0’; ++i);

for(j = 0; str2[j] != ‘\0’; ++j, ++i)

{

str1[i] = str2[j];

}

str1[i] = ‘\0’;

cout << “Resultant string : ” << str1>

return 0;

}

 

JAVA

// Java program to concatenate two strings

import java.util.*;

public class Main

{

public static void main(String args[])

{

String str1, str2;

System.out.print(“\nEnter first string : “);

Scanner sc = new Scanner(System.in);

str1 = sc.nextLine();

System.out.print(“\nEnter second string : “);

str2 = sc.nextLine();

System.out.println(“Resultant string : ” + str1+str2);

}

}

 

PYTHON 3

# Python program to concatenate two strings

s1 = input(“Enter first string : “)

s2 = input(“Enter second string ” )

print(“Resultant string : “,end = “”)

print(s1+s2)

 

Output

Input- Enter first string: Hello Enter second string: World! Output- Resultant string:HelloWorld!

 

Concatenating two strings using pointers

C

// C program to merge two strings using pointers

#include

int main()

{

char str1[100], str2[100];

printf(“\nEnter the first string : “);

gets(str1);

printf(“\nEnter the second string : “);

gets(str2);

char *a = str1;

char *b = str2;

while(*a)

{

a++;

}

while(*b)

{

*a = *b;

b++;

a++;

}

*a = ‘\0’;

printf(“\nResultant String: %s\n “, str1);

return 0;

}

 

C++

// C++ program to merge two strings

#include

using namespace std;

int main()

{

char str1[100], str2[100];

cout << “\nEnter the first string : “;

cin >> str1;

cout << “\nEnter the second string : “;

cin >> str2;

char *a = str1;

char *b = str2;

while(*a)

{

a++;

}

while(*b)

{

*a = *b;

b++;

a++;

}

*a = ‘\0’;

cout << “\nResultant String ” << str1>

return 0;

}

 

Output

Input- Enter first string: Hello Enter second string: World! Output- Resultant string: HelloWorld!

 

Concatenating two strings using library function

C

// C program to concatenate two strings using library functions

#include

#include

int main()

{

char str1[100], str2[100], i, j;

printf(“\nEnter first string: “);

scanf(“%s”, str1);

printf(“\nEnter second string: “);

scanf(“%s”, str2);

printf(“Resultant string : %s”, strcat(str1,str2));

return 0;

}

 

C++

// C++ program to concatenate two strings using library functions

#include

#include

using namespace std;

int main()

{

char str1[100], str2[100], i, j;

cout << “\nEnter first string: “;

cin >> str1;

cout << “\nEnter second string: “;

cin >> str2;

cout << “Resultant string : ” << strcat>

return 0;

}

 

JAVA

// Java program to concatenate two strings using library function

import java.util.*;

public class Main

{

public static void main(String args[])

{

String str1, str2;

System.out.print(“\nEnter first string : “);

Scanner sc = new Scanner(System.in);

str1 = sc.nextLine();

System.out.print(“\nEnter second string : “);

str2 = sc.nextLine();

System.out.println(“Resultant string : ” + str1.concat(str2));

}

}

 

Output

Input- Enter first string: Hello Enter second string: World! Output- Resultant string:HelloWorld!