Program to concatenate two String

Problem statement:- Program to concatenate two String.

Data requirement:-

   Input Data:- str, str2

  Output Data:-count

  Additional Data:- i, j

Program in C

Here is the source code of the C Program to concatenate two strings without using strcat().

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[30];
    printf("Enter your 1st String:");
    gets(str);
    char str2[30];
    printf("Enter your 2nd String:");
    gets(str2);
    int i;
    for(i=0;str[i]!='\0';i++);

        str[i]=' ';
        i++;
      int j;
      printf("After concatenate string is:");
   for(j=0;str2[j]!='\0';j++,i++)
   {
       str[i]=str2[j];
   }
    str[i]='\0';
   printf("%s",str);
}

Input/Output:
Enter your 1st String:cs info
Enter your 2nd String:360 dot com
After concatenate string is:cs info  360 dot com

Program in C++

Here is the source code of the C++ Program to concatenate two strings without using strcat() function.

Code:

#include<iostream>
using namespace std;
int main()
{
    string str,str2;
    cout<<"Enter your 1st String:";
    getline(cin,str);
    cout<<"Enter your 2nd String:";
    getline(cin,str2);
    string str3=str+' '+str2;
    cout<<"After concatenate string is:";
    cout<<str3;
}
}

Input/Output:
Enter your 1st String:c
Enter your 2nd String:plus plus
After concatenate string is:c plus plus


Program in Java

Here is the source code of the Java Program to concatenate two strings.

Code:

import java.util.Scanner;
public class p24 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1,str2;
System.out.println("Enter your 1st String:");
str1=cs.nextLine();
System.out.println("Enter your 2nd String:");
str2=cs.nextLine();
System.out.println("After concatenate string is:");
System.out.println(str1+" "+str2);
    cs.close();
}}

Input/Output:
Enter your 1st String:
java c c 
Enter your 2nd String:
plus plus program
After concatenate string is:
java c c  plus plus program

Program in Python

Here is the source code of the Python Program to concatenate two strings.

Code:
str=input("Enter the 1st String:")
str2=input("Enter the 2nd String:")
print("After concatenate string is:")
print(str+" "+str2)

Input/Output:
Enter the 1st String:python string
Enter the 2nd String:program
After concatenate string is:
python string program


Post a Comment

0 Comments