Java Program to Concatenate two strings without using string functions

[5973 views]




For eg.
Input: 1st String - Hello World
2nd String - Welcome to Java
Output: Hello World Welcome to Java

Concatenate two strings without using string functions using Java

Program 1:

import java.util.*; class StringConcat{ public static void main(String[] args){ int length1,length2; String word1,word2; char[] cword1,cword2,finalword; Scanner sc=new Scanner(System.in); word1=sc.nextLine(); word2=sc.nextLine(); cword1=word1.toCharArray(); cword2=word2.toCharArray(); length1=word1.length(); length2=word2.length(); finalword=new char[length1+length2]; for(int k=0;k<length1;k++){ finalword[k]=cword1[k]; } for(int i=0,k=length1;k<(length1+length2);k++,i++){ finalword[k]=cword2[i]; } for(char word:finalword) System.out.print(word); } }

Program 2:

class ConcatenateString { public static void main(String args[]) { String string1,string2; Scanner sc = new Scanner(System.in); System.out.println("Enter the 1st string"); string1=sc.nextLine(); System.out.println("Enter the 2nd string"); string2=sc.nextLine(); System.out.println("Concatenated String is "); System.out.println(string1+string2); } }
                 



Clear Any Java Interview by Reading our Ebook Once.


Can you clear Java Interview?




Comments










Search Anything:

Sponsored Deals ends in



Technical Quizzes Specially For You:

Search Tags

    Concatenate 2 strings without using java function

    Join 2 strings algorithm without java inbuilt method