Skip to content

Commit 06d9163

Browse files
committed
Update clang-formatting in RailFenceCipher.java
1 parent c50b777 commit 06d9163

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ 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()) return str;
19+
if (rails == 1 || rails >= str.length()){
20+
return str;
21+
}
2022

2123
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
2224
boolean down = true;
2325
// Create a 2D array to represent the rails (rows) and the length of the string (columns).
2426
char[][] strRail = new char[rails][str.length()];
2527

2628
// 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++){
2830
Arrays.fill(strRail[i], '\n');
31+
}
2932

3033
int row = 0; // Start at the first row
3134
int col = 0; // Start at the first column
@@ -35,16 +38,23 @@ public String encrypt(String str, int rails) {
3538
// Fill the rail matrix with characters from the string based on the rail pattern.
3639
while (col < str.length()) {
3740
// Change direction to down when at the first row.
38-
if (row == 0) down = true;
41+
if (row == 0){
42+
down = true;
43+
}
3944
// 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+
}
4148

4249
// Place the character in the current position of the rail matrix.
4350
strRail[row][col] = str.charAt(i);
4451
col++; // Move to the next column.
4552

4653
// Move to the next row based on the direction.
47-
if (down) row++;
54+
if (down){
55+
row++;
56+
}
57+
4858
else row--;
4959

5060
i++;
@@ -57,19 +67,19 @@ public String encrypt(String str, int rails) {
5767
if (ch != '\n') encryptedString.append(ch);
5868
}
5969
}
60-
6170
return encryptedString.toString();
6271
}
63-
6472
// Decrypts the input string using the rail fence cipher method with the given number of rails.
6573
public String decrypt(String str, int rails) {
6674

6775
// 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+
}
6979

7080
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
7181
boolean down = true;
72-
82+
7383
// Create a 2D array to represent the rails (rows) and the length of the string (columns).
7484
char[][] strRail = new char[rails][str.length()];
7585

@@ -111,17 +121,24 @@ public String decrypt(String str, int rails) {
111121

112122
while (col < str.length()) {
113123
// Change direction to down when at the first row.
114-
if (row == 0) down = true;
124+
if (row == 0){
125+
down = true;
126+
}
115127
// 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+
}
118131
// Append the character from the rail matrix to the decrypted string.
119132
decryptedString.append(strRail[row][col]);
120133
col++; // Move to the next column.
121134

122135
// 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+
}
125142
}
126143

127144
return decryptedString.toString();

0 commit comments

Comments
 (0)