I would write a snippet that concats two string(with specific elements) to one. May someone could help me?
My code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String first = sc.nextLine();
String second = sc.nextLine();
StringBuilder builder = new StringBuilder(" ");
for(int i = 0; i < first.length(); i++){
builder.append(first.charAt(i));
builder.append(second.charAt(i));
}
String base = builder.toString();
System.out.println(base);
}
}
Tom,
The problem is that one string is longer than the other one.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String elso = sc.nextLine();
String masodik = sc.nextLine();
String longestString = elso;
String shortestString = masodik;
if (shortestString.length() > longestString.length()){
shortestString = elso;
longestString = masodik;
}
StringBuilder builderem = new StringBuilder(" ");
for(int i = 0; i < shortestString.length(); i++){
builderem.append(shortestString.charAt(i));
builderem.append(longestString.charAt(i));
}
for(int i = shortestString.length(); i < longestString.length(); i++){
builderem.append(longestString.charAt(i));
}
String alapba = builderem.toString();
System.out.println(alapba);
}
}
Please let me know if it is working!