@@ -16,16 +16,19 @@ public class RailFenceCipher {
16
16
public String encrypt (String str , int rails ) {
17
17
18
18
// Base case of single rail or rails are more than the number of characters in the string
19
- if (rails == 1 || rails >=str .length ()) return str ;
19
+ if (rails == 1 || rails >= str .length ()){
20
+ return str ;
21
+ }
20
22
21
23
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
22
24
boolean down = true ;
23
25
// Create a 2D array to represent the rails (rows) and the length of the string (columns).
24
26
char [][] strRail = new char [rails ][str .length ()];
25
27
26
28
// Initialize all positions in the rail matrix with a placeholder character ('\n').
27
- for (int i = 0 ; i < rails ; i ++)
29
+ for (int i = 0 ; i < rails ; i ++){
28
30
Arrays .fill (strRail [i ], '\n' );
31
+ }
29
32
30
33
int row = 0 ; // Start at the first row
31
34
int col = 0 ; // Start at the first column
@@ -35,16 +38,23 @@ public String encrypt(String str, int rails) {
35
38
// Fill the rail matrix with characters from the string based on the rail pattern.
36
39
while (col < str .length ()) {
37
40
// Change direction to down when at the first row.
38
- if (row == 0 ) down = true ;
41
+ if (row == 0 ){
42
+ down = true ;
43
+ }
39
44
// Change direction to up when at the last row.
40
- else if (row == rails - 1 ) down = false ;
45
+ else if (row == rails - 1 ){
46
+ down = false ;
47
+ }
41
48
42
49
// Place the character in the current position of the rail matrix.
43
50
strRail [row ][col ] = str .charAt (i );
44
51
col ++; // Move to the next column.
45
52
46
53
// Move to the next row based on the direction.
47
- if (down ) row ++;
54
+ if (down ){
55
+ row ++;
56
+ }
57
+
48
58
else row --;
49
59
50
60
i ++;
@@ -57,19 +67,19 @@ public String encrypt(String str, int rails) {
57
67
if (ch != '\n' ) encryptedString .append (ch );
58
68
}
59
69
}
60
-
61
70
return encryptedString .toString ();
62
71
}
63
-
64
72
// Decrypts the input string using the rail fence cipher method with the given number of rails.
65
73
public String decrypt (String str , int rails ) {
66
74
67
75
// Base case of single rail or rails are more than the number of characters in the string
68
- if (rails == 1 || rails >=str .length ()) return str ;
76
+ if (rails == 1 || rails >= str .length ()){
77
+ return str ;
78
+ }
69
79
70
80
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
71
81
boolean down = true ;
72
-
82
+
73
83
// Create a 2D array to represent the rails (rows) and the length of the string (columns).
74
84
char [][] strRail = new char [rails ][str .length ()];
75
85
@@ -111,17 +121,24 @@ public String decrypt(String str, int rails) {
111
121
112
122
while (col < str .length ()) {
113
123
// Change direction to down when at the first row.
114
- if (row == 0 ) down = true ;
124
+ if (row == 0 ){
125
+ down = true ;
126
+ }
115
127
// Change direction to up when at the last row.
116
- else if (row == rails - 1 ) down = false ;
117
-
128
+ else if (row == rails - 1 ){
129
+ down = false ;
130
+ }
118
131
// Append the character from the rail matrix to the decrypted string.
119
132
decryptedString .append (strRail [row ][col ]);
120
133
col ++; // Move to the next column.
121
134
122
135
// Move to the next row based on the direction.
123
- if (down ) row ++;
124
- else row --;
136
+ if (down ){
137
+ row ++;
138
+ }
139
+ else {
140
+ row --;
141
+ }
125
142
}
126
143
127
144
return decryptedString .toString ();
0 commit comments