@@ -16,7 +16,7 @@ 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 ()){
19
+ if (rails == 1 || rails >= str .length ()) {
20
20
return str ;
21
21
}
22
22
@@ -38,11 +38,11 @@ public String encrypt(String str, int rails) {
38
38
// Fill the rail matrix with characters from the string based on the rail pattern.
39
39
while (col < str .length ()) {
40
40
// Change direction to down when at the first row.
41
- if (row == 0 ){
41
+ if (row == 0 ) {
42
42
down = true ;
43
43
}
44
44
// Change direction to up when at the last row.
45
- else if (row == rails - 1 ){
45
+ else if (row == rails - 1 ) {
46
46
down = false ;
47
47
}
48
48
@@ -51,11 +51,12 @@ else if (row == rails - 1){
51
51
col ++; // Move to the next column.
52
52
53
53
// Move to the next row based on the direction.
54
- if (down ){
54
+ if (down ) {
55
55
row ++;
56
56
}
57
-
58
- else row --;
57
+ else {
58
+ row --;
59
+ }
59
60
60
61
i ++;
61
62
}
@@ -64,7 +65,9 @@ else if (row == rails - 1){
64
65
StringBuilder encryptedString = new StringBuilder ();
65
66
for (char [] chRow : strRail ) {
66
67
for (char ch : chRow ) {
67
- if (ch != '\n' ) encryptedString .append (ch );
68
+ if (ch != '\n' ) {
69
+ encryptedString .append (ch );
70
+ }
68
71
}
69
72
}
70
73
return encryptedString .toString ();
@@ -73,7 +76,7 @@ else if (row == rails - 1){
73
76
public String decrypt (String str , int rails ) {
74
77
75
78
// Base case of single rail or rails are more than the number of characters in the string
76
- if (rails == 1 || rails >= str .length ()){
79
+ if (rails == 1 || rails >= str .length ()) {
77
80
return str ;
78
81
}
79
82
@@ -90,18 +93,26 @@ public String decrypt(String str, int rails) {
90
93
while (col < str .length ()) {
91
94
92
95
// Change direction to down when at the first row.
93
- if (row == 0 ) down = true ;
96
+ if (row == 0 ) {
97
+ down = true ;
98
+ }
94
99
95
100
// Change direction to up when at the last row.
96
- else if (row == rails - 1 ) down = false ;
101
+ else if (row == rails - 1 ) {
102
+ down = false ;
103
+ }
97
104
98
105
// Mark the current position in the rail matrix.
99
106
strRail [row ][col ] = '*' ;
100
107
col ++; // Move to the next column.
101
108
102
109
// Move to the next row based on the direction.
103
- if (down ) row ++;
104
- else row --;
110
+ if (down ) {
111
+ row ++;
112
+ }
113
+ else {
114
+ row --;
115
+ }
105
116
}
106
117
107
118
int index = 0 ; // Index to track characters from the input string.
@@ -121,22 +132,22 @@ public String decrypt(String str, int rails) {
121
132
122
133
while (col < str .length ()) {
123
134
// Change direction to down when at the first row.
124
- if (row == 0 ){
135
+ if (row == 0 ) {
125
136
down = true ;
126
137
}
127
138
// Change direction to up when at the last row.
128
- else if (row == rails - 1 ){
139
+ else if (row == rails - 1 ) {
129
140
down = false ;
130
141
}
131
142
// Append the character from the rail matrix to the decrypted string.
132
143
decryptedString .append (strRail [row ][col ]);
133
144
col ++; // Move to the next column.
134
145
135
146
// Move to the next row based on the direction.
136
- if (down ){
147
+ if (down ) {
137
148
row ++;
138
149
}
139
- else {
150
+ else {
140
151
row --;
141
152
}
142
153
}
0 commit comments