-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecipherCaesarCode.java
29 lines (23 loc) · 1003 Bytes
/
DecipherCaesarCode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
public class DecipherCaesarCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a ciphertext string: ");
String ciphertext = scanner.nextLine().toUpperCase();
System.out.println("The plaintext string is: " + decryptCaesar(ciphertext));
scanner.close();
}
private static String decryptCaesar(String ciphertext) {
int shift = 3; // Fixed shift value for Caesar's Code
StringBuilder plaintext = new StringBuilder();
for (char ch : ciphertext.toCharArray()) {
if (Character.isLetter(ch)) {
char decryptedChar = (char) ('A' + (ch - 'A' - shift + 26) % 26);
plaintext.append(decryptedChar);
} else {
plaintext.append(ch); // Non-alphabetic characters remain unchanged
}
}
return plaintext.toString();
}
}