-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi3.java
More file actions
executable file
·46 lines (40 loc) · 1.36 KB
/
i3.java
File metadata and controls
executable file
·46 lines (40 loc) · 1.36 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
/*
* Challenge I3
* https://www.reddit.com/r/dailyprogrammer/comments/pkwb1/2112012_challenge_3_intermediate/
*/
import java.util.ArrayList;
public class Main {
public static String encode(String text) {
char[] characters = text.toCharArray();
char[] char_new = new char[characters.length];
for (int i = 0; i < characters.length; ++i) {
int num = (int) (characters[i]) - 32 + i;
if (num > 94) {
num = (num % 94);
}
char_new[i] = (char) (num+32);
}
String encrypted = new String(char_new);
return encrypted;
}
public static String decode(String text){
char[] characters = text.toCharArray();
char[] decoded = new char[characters.length];
for (int i = 0; i < characters.length; ++i){
int num = (int) (characters[i]) - 32 - i;
if (num < 0){
int shift_from_end = Math.abs(num)%94;
num = 94 - shift_from_end;
}
decoded[i] = (char)(num+32);
}
return new String(decoded);
}
public static void main(String[] args) {
String t = "I am not a half-baked potato! I'm a couch potato!";
System.out.println(t);
String e = encode(t);
System.out.println(e);
System.out.println(decode(e));
}
}