-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSCCompW6.java
207 lines (190 loc) · 4.46 KB
/
CSCCompW6.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/***
* CSC Competition Question: Week 6
*
* Open until March 30 11pm
*
* Description:
*
* Given a sentence and either the word "encrypt" or "decrypt", encrypt or
* decrpyt the given sentence. The following is the encryption scheme.
*
* 1) Remove the spaces from the sentence
*
* 2) Create a grid. If the length of the sentence, L, is 54 characters long.
* The square root of 54 is between 7 and 8, so it is written in the form of
* a grid with 7 rows and 8 columns. Ensure that number of rows multiplied by
* the number of columns >= L If multiple grids satisfy the above conditions,
* choose the one with the minimum area.
*
* 3) The encoded message is obtained by displaying the characters in a column,
* inserting a space, and then displaying the next column and inserting a space,
* and so on.
*
* Sample Input: Ohana means family encrypt
*
* grid is 4 x 4 -> Ohan amea nsfa mily
*
* Sample Output: Oanm hmsi aefl naay
*
*
* Sample Input: Oanm hmsi aefl naay decrypt
*
* Sample Output: Ohanameansfamily
*
* Note: The spaces do not have to be added in after decryption
*
* Algorithm:
*
* 1: Take String and instruction from user input.
*
* 2: Determine instruction command.
*
* 3: Execute Command:
*
* A) Encryption Algorithm:
*
* i: Remove whitespace from the input String.
*
* ii: Find the size of the encryption grid.
*
* iii: Create and fill the encryption grid:
*
* a) A nested for-loop iterates through the String and add's the character at position zero to a 2D array.
* This charcter is then removed from the String.
*
* iv: Print the encrypted message
*
* B) Decryption Method:
*
* i: Remove whitespace from the encrypted String for part ii.
*
* ii: Find the size of the decryption grid.
*
* iii: Create and fill the decryption grid:
*
* Note: I have used the .split() method to separate the encrypted String into individual words and then added
* a single word to a single column. This process prevents characters being misplaced as will occur if a
* traditional nested-loop approach is used.
*
* iv: Print the encrypted message
*
***/
import java.util.Scanner;
public class CSCCompW6
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
// 1:
String input = scan.nextLine();
String command = scan.nextLine();
command = command.toLowerCase();
scan.close();
// 2:
if(command.equals("encrypt"))
{
encrypt(input);
}
else if(command.equals("decrypt"))
{
decrypt(input);
}
else
{
System.out.println("Invalid command - Try again");
}
} //End main()
public static void encrypt(String input) // A)
{ // i:
String x = new String("");
for(String y: input.split(" "))
{
x += y;
}
// ii:
int length = x.length();
int rows = (int)Math.sqrt(length);
int cols = rows;
while(length > (rows * cols))
{
cols++;
}
// iii:
String[][] encrypt = new String[rows][cols];
for(int i = 0; i < rows; i++)
{ // a)
for(int j = 0; j < cols; j++)
{
if(x.length() > 1)
{
encrypt[i][j] = x.substring(0, 1);
x = x.substring(1);
}
else if(x.length() == 1)
{
encrypt[i][j] = x; break;
}
}
} //End nested loop
// iv:
for(int i = 0; i < cols; i++)
{
for(int j = 0; j < rows; j++)
{
if(encrypt[j][i] != null)
{
System.out.print(encrypt[j][i]);
}
}
System.out.print(" ");
}
} //End encrypt()
public static void decrypt(String input) // B)
{
// i:
String x = new String("");
for(String y: input.split(" "))
{
x += y;
}
// ii:
int length = x.length();
int rows = (int)Math.sqrt(length);
int cols = rows;
while(length > (rows * cols))
{
cols++;
}
// iii:
String[][] decrypt = new String[rows][cols];
int pos = 0;
for(String y: input.split(" "))
{
String z = y;
for(int i = 0; i < rows; i++)
{
if(z.length() > 1)
{
decrypt[i][pos] = z.substring(0, 1);
z = z.substring(1);
}
else if(z.length() == 1)
{
decrypt[i][pos] = z; break;
}
}
pos++;
}
// iv:
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
if(decrypt[i][j] != null)
{
System.out.print(decrypt[i][j]);
}
}
}
} //End decrypt()
} //End class