-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString18.java
More file actions
27 lines (22 loc) · 821 Bytes
/
String18.java
File metadata and controls
27 lines (22 loc) · 821 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
import java.util.*;
public class String18 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String message = sc.nextLine();
StringBuilder encrypted = new StringBuilder();
for (char ch : message.toCharArray()) {
if (ch >= 'A' && ch <= 'Z') {
char newChar = (char) ((ch - 'A' + 3) % 26 + 'A');
encrypted.append(newChar);
} else if (ch >= 'a' && ch <= 'z') {
char newChar = (char) ((ch - 'a' + 3) % 26 + 'a');
encrypted.append(newChar);
} else {
encrypted.append(ch);
}
}
System.out.println("Original: " + message);
System.out.println("Encrypted: " + encrypted.toString());
sc.close();
}
}