Write java program for String Concatenation.

Write code to get two strings as input and If strings are of the same length simply append them together and return the final string.

If given strings are of different lengths, remove starting characters from the long string so that both strings are of the same length then append them together and return the final string. The return type of the output is a string which is the concatenated string.

Input and Output Format

  • Input consists of two strings.
  • The output is a string.

Refer sample output for formatting specifications

Sample Input 1:

Hello

hi

Sample Output 1:

lohi

Sample Input 2:

Hello

Delhi

Sample Output 2:

HelloDelhi

String Concatenation Program in Java

Following are the steps to concatenate string in Java:

  • Input two strings from the user.
  • Pass both the string to getvalues() method.
  • Inside the method, create an empty string buffer. Now, store the length of both the string in l1 and l2 variables.
  • Now, check for the below mention condition:
    • If both the length of the string is equal, then append both the string using the append() method to the string buffer.
    • If the length of string one is greater than other string lengths, then extract the substring of string and append it with another string to the string buffer.
    • If the length of string one is less than other string lengths, then extract the substring of string and append it with another string to the string buffer.
  • At last, print the string in a string buffer.
import java.util.Scanner;

public class Main {
  /**
   * @param args
   */
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    String s2 = sc.nextLine();

    getvalues(s1, s2);
  }

  public static void getvalues(String s1, String s2) {
    StringBuffer sb = new StringBuffer();
    int l1 = s1.length();
    int l2 = s2.length();
    if (l1 == l2) {
      sb.append(s1).append(s2);
    } else if (l1 > l2) {
      sb.append(s1.substring(s1.length() - s2.length(), s1.length()))
          .append(s2);
    } else if (l1 < l2) {
      sb.append(s1).append(
          s2.substring(s2.length() - s1.length(), s2.length()));
    }
    System.out.println(sb);
  }

}

Output

String Concatenation without vowels

Write a Java code to concatenate two strings. Take two strings from the user and join them after removing vowels from both the string.

Input and Output Format

  • Input consists of two strings.
  • The output is a string.

Refer sample output for formatting specifications

Sample Input 1:

interesting

hello

Sample Output 1:

ntrstnghll

Note: Enter input in lowercase

Program to concatenate string in Java

Following are the steps to join string in Java:

  • Input two strings from the user.
  • Pass both the string to getvalues() method.
  • Inside the method, create an empty string buffer.
  • Now, traverse over the first string and check for vowel. If the character is not vowel then append that character to the string buffer.
  • Repeat the same process for the second string also.
  • At last, print the string in a string buffer.
package com.company;

import java.util.Scanner;

public class StringJoin {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.nextLine();
        String s2 = scanner.nextLine();
        getvalues(s1,s2);
    }

    private static void getvalues(String s1, String s2) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0 ; i <s1.length();i++){
            if(s1.charAt(i)!='a' && s1.charAt(i)!='e' && s1.charAt(i)!='i' && s1.charAt(i)!='o' && s1.charAt(i)!='u' ){
                sb.append(s1.charAt(i));
            }
        }
        for(int i = 0 ; i <s2.length();i++){
            if(s2.charAt(i)!='a' && s2.charAt(i)!='e' && s2.charAt(i)!='i' && s2.charAt(i)!='o' && s2.charAt(i)!='u' ){
                sb.append(s2.charAt(i));
            }
        }
        System.out.println(sb);
    }
}

Output

Concatenate String using concat() method and ‘+’ Operator

concat(): This method joins two string and return them.

Java Program to concatenate string using concat() method

Here, we will write a program to join strings using the concat() method.

package com.company;

import java.util.Scanner;

public class StrinPractice {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.nextLine();
        String s2 = scanner.nextLine();
        System.out.println(s1.concat(s2));
    }
}

Output

‘+’ Operator is also used to concatenate string but the difference here is we can add a string on either side. Also, we can add multiple strings, unlike the concat() method which can take up to one string only.

Java Program to concatenate string using + Operator

Here, we will write a program to join strings using the + operator.

package com.company;

import java.util.Scanner;

public class StrinPractice {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.nextLine();
        String s2 = scanner.nextLine();
        System.out.println(s1+s2);
        System.out.println(s2+s1);
    }
}

Output

Thus, in this way, we can concatenate string using different ways.