Home > other >  How to merge characters of two string in a pattern of 5*5 in java
How to merge characters of two string in a pattern of 5*5 in java

Time:03-07

Having the following two strings, “abcde” and “12345”, write the needed code to display the output as shown below:

a1 a2 a3 a4 a5  
b1 b2 b3 b4 b5  
c1 c2 c3 c4 c5  
d1 d2 d3 d4 d5  
e1 e2 e3 e4 e5 

i dont know how to merge the first string with the second one . i am only getting this output :

a1 b2 c3 d4 
public class EX3 {
    public static void main (String args[]){
        String str1="abcde";
        String str2="12345";
        int rows = 5;
        int columns = 5;
        
        for (int i = 1; i<=rows;i  ){
            for(int j=1;j<=columns;j  ){
                for(int k= 1;k<i;k  ){
                    k=str1.charAt(i);
                    System.out.print(str1.charAt(k));
                }
            }
        }

}
}

CodePudding user response:

You can split the strings and use stream:

public static List<String> merge(String str1, String str2) {
    return Arrays.stream(str1.split(""))
            .map(letter -> Arrays.stream(str2.split(""))
                    .map(number -> letter   number)
                    .collect(Collectors.joining(" ")))
            .toList();
}

Test:

String str1 = "abcde";
String str2 = "12345";

List<String> list = merge(str1, str2)

list.forEach(System.out::println);

Output:

a1 a2 a3 a4 a5
b1 b2 b3 b4 b5
c1 c2 c3 c4 c5
d1 d2 d3 d4 d5
e1 e2 e3 e4 e5

If you don't want to use stream you can do:

public static void merge(String str1, String str2) {

    String[] arr1 = str1.split("");
    String[] arr2 = str2.split("");

    int rows = arr1.length;
    int cols = arr2.length;

    for (int i = 0; i < rows; i  ) {

        StringBuilder sb = new StringBuilder();

        for (int j = 0; j < cols; j  ) {

            sb.append(arr1[i]).append(arr2[i]);

            if (j < cols - 1) {
                sb.append(" ");
            }
        }

        System.out.println(sb.toString());
    }

}

CodePudding user response:

The third nested loop by k is not needed, for a rectangular/square "matrix" two loops are enough.

Also, rows / columns should be initialized with the actual lengths of str1 and str2 respectively.

So, if the question is only about output, a simple loop-based solution may look as follows:

String str1 = "abcde";
String str2 = "12345";

for (int i = 0, rows = str1.length(); i < rows; i  ) {
    for (int j = 0, columns = str2.length(); j < columns; j  ) {
        if (j > 0) { // print space after before the next column
            System.out.print(' ');
        }
        System.out.print(str1.charAt(i)); // print letter (row)
        System.out.print(str2.charAt(j)); // print digit (column)
    }
    System.out.println();
}

Output:

a1 a2 a3 a4 a5 
b1 b2 b3 b4 b5 
c1 c2 c3 c4 c5 
d1 d2 d3 d4 d5 
e1 e2 e3 e4 e5 

CodePudding user response:

One possibility would be do do the following:

  • allocate a result array of arrays;
  • then do a nested iteration of the Strings
  • build each row and assign to the result array.
  • then print them out using join to separate by spaces.
String str1 = "abcde";
String str2 = "12345";
String[][] result = new String[str1.length()][];
int row = 0, col = 0;
for (char c1 : str1.toCharArray()) {
    String[] r = new String[str2.length()]; 
    for (char c2 : str2.toCharArray()) {
        r[col  ] = Character.toString(c1) Character.toString(c2);
    }
    result[row  ] = r;
    col = 0;
}
for (String[] r : result) {
    System.out.println(String.join(" ", r));
}

  •  Tags:  
  • java
  • Related