public class Tarea2022Z_ZigZag {
public static String[][] buildMatriz(String word, int rows) { String[][] matriz = new String[rows][word.length()];
clear(matriz);
boolean down = true;
int j = 0;
for (int i = 0; i < word.length(); i++) { matriz[j][i] = String.valueOf(word.charAt(i));
if (down) { if (j + 1 < rows) { j++;
} else { down = false;
j--;
}
} else { if (j - 1 >= 0) { j--;
} else { down = true;
j++;
}
}
}
return matriz;
}
public static void clear(String[][] matriz) { for (String[] matriz1 : matriz) { for (int j = 0; j < matriz1.length; j++) { matriz1[j] = "";
}
}
}
public static void main(String[] args) { String word = "ABCDEFGHIJKL";
int rows = 4;
// String word = "PROGRAMAR";
// int rows = 3;
String[][] matriz = buildMatriz(word, rows);
for (int i = 0; i < matriz.length; i++) { for (int j = 0; j < matriz[i].length; j++) { System.out.print(matriz[i][j] + "\t");
}
System.out.println();
}
}
}