-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCode.java
More file actions
30 lines (23 loc) · 994 Bytes
/
CaesarCode.java
File metadata and controls
30 lines (23 loc) · 994 Bytes
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
30
import java.util.Scanner;
public class CaesarCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a plaintext string: ");
String plaintext = scanner.nextLine().toUpperCase();
System.out.println("The ciphertext string is: " + encryptCaesar(plaintext));
scanner.close();
}
private static String encryptCaesar(String plaintext) {
int shift = 3; // Fixed shift value for Caesar's Code
StringBuilder ciphertext = new StringBuilder();
for (char ch : plaintext.toCharArray()) {
if (Character.isLetter(ch)) {
char encryptedChar = (char) ('A' + (ch - 'A' + shift) % 26);
ciphertext.append(encryptedChar);
} else {
ciphertext.append(ch); // Non-alphabetic characters remain unchanged
}
}
return ciphertext.toString();
}
}