Skip to content

Instantly share code, notes, and snippets.

@sampletext32
Created April 25, 2021 14:14
Show Gist options
  • Save sampletext32/a849ed458a97a733dfb1e27aa5f2beac to your computer and use it in GitHub Desktop.
Save sampletext32/a849ed458a97a733dfb1e27aa5f2beac to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
String source = "Hello world";
char[][] matrix = toMatrix(source);
printMatrix(matrix);
}
public static void printMatrix(char[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static char[][] toMatrix(String src) {
char[][] chars = new char[4][4];
for (int i = 0; i < src.length(); i++) {
int row = i / 4;
int col = i % 4;
chars[row][col] = src.charAt(i);
}
return chars;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment