10
10
*/
11
11
12
12
13
- public class RailFenceCipher {
13
+ public class RailFenceCipher {
14
14
15
15
// Encrypts the input string using the rail fence cipher method with the given number of rails.
16
16
public String encrypt (String str , int rails ) {
@@ -26,7 +26,7 @@ public String encrypt(String str, int rails) {
26
26
char [][] strRail = new char [rails ][str .length ()];
27
27
28
28
// Initialize all positions in the rail matrix with a placeholder character ('\n').
29
- for (int i = 0 ; i < rails ; i ++){
29
+ for (int i = 0 ; i < rails ; i ++) {
30
30
Arrays .fill (strRail [i ], '\n' );
31
31
}
32
32
@@ -49,7 +49,6 @@ else if (row == rails - 1) {
49
49
// Place the character in the current position of the rail matrix.
50
50
strRail [row ][col ] = str .charAt (i );
51
51
col ++; // Move to the next column.
52
-
53
52
// Move to the next row based on the direction.
54
53
if (down ) {
55
54
row ++;
@@ -79,7 +78,6 @@ public String decrypt(String str, int rails) {
79
78
if (rails == 1 || rails >= str .length ()) {
80
79
return str ;
81
80
}
82
-
83
81
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
84
82
boolean down = true ;
85
83
@@ -91,12 +89,10 @@ public String decrypt(String str, int rails) {
91
89
92
90
// Mark the pattern on the rail matrix using '*'.
93
91
while (col < str .length ()) {
94
-
95
92
// Change direction to down when at the first row.
96
93
if (row == 0 ) {
97
94
down = true ;
98
95
}
99
-
100
96
// Change direction to up when at the last row.
101
97
else if (row == rails - 1 ) {
102
98
down = false ;
@@ -105,12 +101,10 @@ else if (row == rails - 1) {
105
101
// Mark the current position in the rail matrix.
106
102
strRail [row ][col ] = '*' ;
107
103
col ++; // Move to the next column.
108
-
109
104
// Move to the next row based on the direction.
110
105
if (down ) {
111
106
row ++;
112
- }
113
- else {
107
+ } else {
114
108
row --;
115
109
}
116
110
}
@@ -142,12 +136,10 @@ else if (row == rails - 1) {
142
136
// Append the character from the rail matrix to the decrypted string.
143
137
decryptedString .append (strRail [row ][col ]);
144
138
col ++; // Move to the next column.
145
-
146
139
// Move to the next row based on the direction.
147
140
if (down ) {
148
141
row ++;
149
- }
150
- else {
142
+ } else {
151
143
row --;
152
144
}
153
145
}
0 commit comments