Skip to content

Commit 6d30942

Browse files
committed
Fix code formatting in RailFenceCipher.java
1 parent bf7196c commit 6d30942

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/main/java/com/thealgorithms/ciphers/RailFenceCipher.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class RailFenceCipher {
1616
public String encrypt(String str, int rails) {
1717

1818
// 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()) {
2020
return str;
2121
}
2222

@@ -38,11 +38,11 @@ public String encrypt(String str, int rails) {
3838
// Fill the rail matrix with characters from the string based on the rail pattern.
3939
while (col < str.length()) {
4040
// Change direction to down when at the first row.
41-
if (row == 0){
41+
if (row == 0) {
4242
down = true;
4343
}
4444
// Change direction to up when at the last row.
45-
else if (row == rails - 1){
45+
else if (row == rails - 1) {
4646
down = false;
4747
}
4848

@@ -51,11 +51,12 @@ else if (row == rails - 1){
5151
col++; // Move to the next column.
5252

5353
// Move to the next row based on the direction.
54-
if (down){
54+
if (down) {
5555
row++;
5656
}
57-
58-
else row--;
57+
else {
58+
row--;
59+
}
5960

6061
i++;
6162
}
@@ -64,7 +65,9 @@ else if (row == rails - 1){
6465
StringBuilder encryptedString = new StringBuilder();
6566
for (char[] chRow : strRail) {
6667
for (char ch : chRow) {
67-
if (ch != '\n') encryptedString.append(ch);
68+
if (ch != '\n') {
69+
encryptedString.append(ch);
70+
}
6871
}
6972
}
7073
return encryptedString.toString();
@@ -73,7 +76,7 @@ else if (row == rails - 1){
7376
public String decrypt(String str, int rails) {
7477

7578
// 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()) {
7780
return str;
7881
}
7982

@@ -90,18 +93,26 @@ public String decrypt(String str, int rails) {
9093
while (col < str.length()) {
9194

9295
// Change direction to down when at the first row.
93-
if (row == 0) down = true;
96+
if (row == 0) {
97+
down = true;
98+
}
9499

95100
// 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+
}
97104

98105
// Mark the current position in the rail matrix.
99106
strRail[row][col] = '*';
100107
col++; // Move to the next column.
101108

102109
// 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+
}
105116
}
106117

107118
int index = 0; // Index to track characters from the input string.
@@ -121,22 +132,22 @@ public String decrypt(String str, int rails) {
121132

122133
while (col < str.length()) {
123134
// Change direction to down when at the first row.
124-
if (row == 0){
135+
if (row == 0) {
125136
down = true;
126137
}
127138
// Change direction to up when at the last row.
128-
else if (row == rails - 1){
139+
else if (row == rails - 1) {
129140
down = false;
130141
}
131142
// Append the character from the rail matrix to the decrypted string.
132143
decryptedString.append(strRail[row][col]);
133144
col++; // Move to the next column.
134145

135146
// Move to the next row based on the direction.
136-
if (down){
147+
if (down) {
137148
row++;
138149
}
139-
else{
150+
else {
140151
row--;
141152
}
142153
}

0 commit comments

Comments
 (0)