-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExchangeCipher.java
31 lines (23 loc) · 978 Bytes
/
ExchangeCipher.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
30
31
import java.util.Scanner;
public class ExchangeCipher {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a plaintext string: ");
String plaintext = scanner.nextLine().toUpperCase();
String ciphertext = encryptExchangeCipher(plaintext);
System.out.println("The ciphertext string is: " + ciphertext);
scanner.close();
}
private static String encryptExchangeCipher(String plaintext) {
StringBuilder ciphertext = new StringBuilder();
for (char ch : plaintext.toCharArray()) {
if (Character.isLetter(ch)) {
char encryptedChar = (char) ('A' + ('Z' - ch));
ciphertext.append(encryptedChar);
} else {
ciphertext.append(ch); // Non-alphabetic characters remain unchanged
}
}
return ciphertext.toString();
}
}