Update Caesar.java
This commit is contained in:
parent
8b243667d7
commit
9060d2da29
@ -69,12 +69,12 @@ public class Caesar {
|
|||||||
final int length = encryptedMessage.length();
|
final int length = encryptedMessage.length();
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
char current = encryptedMessage.charAt(i);
|
char current = encryptedMessage.charAt(i);
|
||||||
if (current >= 'A' && current <= 'Z') {
|
if (IsCapitalLatinLetter(current)) {
|
||||||
|
|
||||||
current -= shift;
|
current -= shift;
|
||||||
decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters
|
decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters
|
||||||
|
|
||||||
} else if (current >= 'a' && current <= 'z') {
|
} else if (IsSmallLatinLetter(current)) {
|
||||||
|
|
||||||
current -= shift;
|
current -= shift;
|
||||||
decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters
|
decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters
|
||||||
@ -86,6 +86,24 @@ public class Caesar {
|
|||||||
return decoded;
|
return decoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param c
|
||||||
|
* @return true if character is capital Latin letter or false for others
|
||||||
|
*/
|
||||||
|
private static boolean IsCapitalLatinLetter(char c) {
|
||||||
|
return c >= 'A' && c <= 'Z';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param c
|
||||||
|
* @return true if character is small Latin letter or false for others
|
||||||
|
*/
|
||||||
|
private static boolean IsSmallLatinLetter(char c) {
|
||||||
|
return c >= 'a' && c <= 'z';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @deprecated TODO remove main and make JUnit Testing
|
* @deprecated TODO remove main and make JUnit Testing
|
||||||
@ -99,10 +117,13 @@ public class Caesar {
|
|||||||
int shift = input.nextInt() % 26;
|
int shift = input.nextInt() % 26;
|
||||||
System.out.println("(E)ncode or (D)ecode ?");
|
System.out.println("(E)ncode or (D)ecode ?");
|
||||||
char choice = input.next().charAt(0);
|
char choice = input.next().charAt(0);
|
||||||
if (choice == 'E' || choice == 'e') {
|
switch (choice) {
|
||||||
|
case 'E':
|
||||||
|
case 'e':
|
||||||
System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle
|
System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle
|
||||||
}
|
break;
|
||||||
if (choice == 'D' || choice == 'd') {
|
case 'D':
|
||||||
|
case 'd':
|
||||||
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift));
|
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user