-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphaCipher.java
More file actions
86 lines (59 loc) · 1.63 KB
/
Copy pathAlphaCipher.java
File metadata and controls
86 lines (59 loc) · 1.63 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package dailyProgrammer;
import java.util.Scanner;
public class AlphaCipher {
public static void main(String[] args)
{
char[][] Alpha = generateArray();
String codeWord;
String codeMsg;
Scanner codeW = new Scanner(System.in);
Scanner message = new Scanner(System.in);
System.out.println("Enter memorized word: ");
codeWord = codeW.next();
System.out.println("Enter message(no spaces): ");
codeMsg = message.next();
int codeLen = codeWord.length();
int msgLen = codeMsg.length();
String repeated = new String(new char[codeMsg.length()/codeWord.length()]).replace("\0", codeWord);
if(repeated.length() < codeMsg.length())
{
int temp;
temp = codeMsg.length() - repeated.length();
repeated = repeated + codeWord.substring(0,temp);
}
System.out.println(" " + repeated);
System.out.println(" " + codeMsg);
for(int i=0;i<codeMsg.length();i++)
{
System.out.print(Alpha[convert(repeated.charAt(i))][convert(codeMsg.charAt(i))]);
}
}
public static char[][] generateArray()
{
char alphaArr[][] = new char[26][26];
for(int i = 0; i < 26; i++)
{
for(int j = 0; j < 26; j++)
{
int lett = i + j;
if(lett >= 26)
lett = lett - 26;
lett = lett + 65;
char letter = (char)lett;
alphaArr[i][j] = letter;
}
}
return alphaArr;
}
public static int convert(char a)
{
int i = 0;
char[] array = "abcdefghijklmnopqrstuvwxyz".toCharArray();
for(int j=0; j < 26; j++)
{
if(array[j] == a)
i = j;
}
return i;
}
}