mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 14:04:17 +08:00
Format code with prettier (#3375)
This commit is contained in:
@ -25,21 +25,16 @@ public class Caesar {
|
||||
|
||||
final int length = message.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
|
||||
// int current = message.charAt(i); //using char to shift characters because ascii
|
||||
// is in-order latin alphabet
|
||||
char current = message.charAt(i); // Java law : char + int = char
|
||||
|
||||
if (IsCapitalLatinLetter(current)) {
|
||||
|
||||
current += shift;
|
||||
encoded.append((char) (current > 'Z' ? current - 26 : current)); // 26 = number of latin letters
|
||||
|
||||
} else if (IsSmallLatinLetter(current)) {
|
||||
|
||||
current += shift;
|
||||
encoded.append((char) (current > 'z' ? current - 26 : current)); // 26 = number of latin letters
|
||||
|
||||
} else {
|
||||
encoded.append(current);
|
||||
}
|
||||
@ -62,15 +57,11 @@ public class Caesar {
|
||||
for (int i = 0; i < length; i++) {
|
||||
char current = encryptedMessage.charAt(i);
|
||||
if (IsCapitalLatinLetter(current)) {
|
||||
|
||||
current -= shift;
|
||||
decoded.append((char) (current < 'A' ? current + 26 : current)); // 26 = number of latin letters
|
||||
|
||||
} else if (IsSmallLatinLetter(current)) {
|
||||
|
||||
current -= shift;
|
||||
decoded.append((char) (current < 'a' ? current + 26 : current)); // 26 = number of latin letters
|
||||
|
||||
} else {
|
||||
decoded.append(current);
|
||||
}
|
||||
@ -91,12 +82,13 @@ public class Caesar {
|
||||
private static boolean IsSmallLatinLetter(char c) {
|
||||
return c >= 'a' && c <= 'z';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string array which contains all the possible decoded combination.
|
||||
*/
|
||||
public static String[] bruteforce(String encryptedMessage) {
|
||||
String[] listOfAllTheAnswers = new String[27];
|
||||
for (int i=0; i<=26; i++) {
|
||||
for (int i = 0; i <= 26; i++) {
|
||||
listOfAllTheAnswers[i] = decode(encryptedMessage, i);
|
||||
}
|
||||
|
||||
@ -117,24 +109,32 @@ public class Caesar {
|
||||
System.out.println("Please enter the shift number");
|
||||
shift = input.nextInt() % 26;
|
||||
System.out.println(
|
||||
"ENCODED MESSAGE IS \n" + encode(message, shift)); // send our function to handle
|
||||
"ENCODED MESSAGE IS \n" + encode(message, shift)
|
||||
); // send our function to handle
|
||||
break;
|
||||
case 'D':
|
||||
case 'd':
|
||||
System.out.println("Please enter the shift number");
|
||||
shift = input.nextInt() % 26;
|
||||
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift));
|
||||
System.out.println(
|
||||
"DECODED MESSAGE IS \n" + decode(message, shift)
|
||||
);
|
||||
break;
|
||||
case 'B':
|
||||
case 'b':
|
||||
String[] listOfAllTheAnswers = bruteforce(message);
|
||||
for (int i =0; i<=26; i++) {
|
||||
System.out.println("FOR SHIFT " + String.valueOf(i) + " decoded message is " + listOfAllTheAnswers[i]);
|
||||
for (int i = 0; i <= 26; i++) {
|
||||
System.out.println(
|
||||
"FOR SHIFT " +
|
||||
String.valueOf(i) +
|
||||
" decoded message is " +
|
||||
listOfAllTheAnswers[i]
|
||||
);
|
||||
}
|
||||
default:
|
||||
System.out.println("default case");
|
||||
}
|
||||
|
||||
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user