-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstitutionEncode.java
More file actions
39 lines (34 loc) · 1.07 KB
/
SubstitutionEncode.java
File metadata and controls
39 lines (34 loc) · 1.07 KB
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
32
33
34
35
36
37
38
39
import java.io.File;
import java.util.Scanner;
public class SubstitutionEncode {
public static void main(String[] args) throws Exception {
// this one we get the key from "key.txt"
// and encode the message from input.
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] key = new char[26];
File file = new File("key.txt");
Scanner fs = new Scanner(file);
Scanner kb = new Scanner(System.in);
char temp = ' ';
String result = "";
System.out.println("Enter the message to be encoded");
char[] message = kb.nextLine().toCharArray();
kb.close();
for(int i = 0; i < key.length; i++) {
key[i] = fs.nextLine().charAt(4);
}
fs.close();
for(int i = 0; i < message.length; i++) {
for(int j = 0; j < alphabet.length; j++) {
if(message[i] == alphabet[j]) {
temp = key[j];
}
else if(message[i] == ' ') {
temp = ' ';
}
}
result = result + temp;
}
System.out.println(result);
}
}