-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiamondCipherProblem.java
More file actions
57 lines (45 loc) · 1.63 KB
/
DiamondCipherProblem.java
File metadata and controls
57 lines (45 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Arrays;
public class DiamondCipherProblem {
public static char[][] encryptDiamondCipher(char[][] table) {
int n = table.length;
int m = table[0].length;
char[][] diamond = new char[2 * n][n];
// Fill the diamond with empty spaces
for (char[] row : diamond) {
Arrays.fill(row, ' ');
}
// Populate the diamond with characters from the table
for (int i = 0; i < n; i++) {
int rowStart = i / 2;
for (int j = 0; j < m; j++) {
diamond[rowStart + j][i] = table[i][j];
}
}
// Populate the diagonals of the diamond
for (int i = 0; i < n; i++) {
int colStart = n - i - 1;
for (int j = 0; j <= i; j++) {
diamond[n + j][colStart + j] = table[i][j];
}
}
return diamond;
}
public static void printDiamond(char[][] diamond) {
for (char[] row : diamond) {
for (char cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
char[][] table = {
{'a', 'b', 'c', 'd'},
{'e', 'f', 'g', 'h'},
{'i', 'j', 'k', 'l'},
{'m', 'n', 'o', 'p'}
};
char[][] encryptedTable = encryptDiamondCipher(table);
printDiamond(encryptedTable);
}
}