Skip to content

Commit 9060d2d

Browse files
author
khalil2535
committed
Update Caesar.java
1 parent 8b24366 commit 9060d2d

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

ciphers/Caesar.java

+28-7
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ public static String decode(String encryptedMessage, int shift) {
6969
final int length = encryptedMessage.length();
7070
for (int i = 0; i < length; i++) {
7171
char current = encryptedMessage.charAt(i);
72-
if (current >= 'A' && current <= 'Z') {
72+
if (IsCapitalLatinLetter(current)) {
7373

7474
current -= shift;
7575
decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters
7676

77-
} else if (current >= 'a' && current <= 'z') {
77+
} else if (IsSmallLatinLetter(current)) {
7878

7979
current -= shift;
8080
decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters
@@ -86,6 +86,24 @@ public static String decode(String encryptedMessage, int shift) {
8686
return decoded;
8787
}
8888

89+
/**
90+
*
91+
* @param c
92+
* @return true if character is capital Latin letter or false for others
93+
*/
94+
private static boolean IsCapitalLatinLetter(char c) {
95+
return c >= 'A' && c <= 'Z';
96+
}
97+
98+
/**
99+
*
100+
* @param c
101+
* @return true if character is small Latin letter or false for others
102+
*/
103+
private static boolean IsSmallLatinLetter(char c) {
104+
return c >= 'a' && c <= 'z';
105+
}
106+
89107
/**
90108
*
91109
* @deprecated TODO remove main and make JUnit Testing
@@ -99,11 +117,14 @@ public static void main(String[] args) {
99117
int shift = input.nextInt() % 26;
100118
System.out.println("(E)ncode or (D)ecode ?");
101119
char choice = input.next().charAt(0);
102-
if (choice == 'E' || choice == 'e') {
103-
System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle
104-
}
105-
if (choice == 'D' || choice == 'd') {
106-
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift));
120+
switch (choice) {
121+
case 'E':
122+
case 'e':
123+
System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle
124+
break;
125+
case 'D':
126+
case 'd':
127+
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift));
107128
}
108129
}
109130

0 commit comments

Comments
 (0)