-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblema23.java
More file actions
83 lines (70 loc) · 2.81 KB
/
Copy pathProblema23.java
File metadata and controls
83 lines (70 loc) · 2.81 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
import acm.program.CommandLineProgram;
public class Problema23 extends CommandLineProgram{
public String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public void run() {
String msg = readLine("Dame un mensaje: ");
int d = readInt("Dame un numero: ");
// String msg= "Hola que tal";
// int d = 4;
String encodeMsg = encodeCaesar(msg, d);
println(encodeMsg);
msg = encodeMsg;
d = -d;
String decodeMsg = encodeCaesar(msg, d);
println(decodeMsg);
}
private String encodeCaesar(String msg, int d) {
StringBuilder encodedMsg = new StringBuilder();
for (int i = 0; i < msg.length(); i++){
encodedMsg.append(
isLower(msg.charAt(i), d) != '\0' ?
isLower(msg.charAt(i), d) :
isUpper(msg.charAt(i), d) != '\0' ?
isUpper(msg.charAt(i), d) :
' '
);
}
return encodedMsg.toString();
}
private char isLower(char c, int d) {
String chars = ALPHABET; // 27
int originalD = d;
for(int i = 0; i < chars.length(); i++){
d = originalD;
if(d>=0 &&( chars.length()-1 - i ) < d){ // en caso que sea positivo i la d supera a la diferencia de distancia entre i y el length
d = (d-1) - (chars.length() - 1 - i);
}else if(d < 0 && (chars.length()-1)-((chars.length()-1)-i) < d*(-1)){ // un caso similar pero con negativos.
d += chars.length();
}
else{
d += i;
}
if(c == chars.charAt(i)){
return chars.charAt(d);
}
}
return '\0';
}
public char isUpper(char c, int d){
String chars = ALPHABET.toUpperCase();
int originalD = d;
for(int i = 0; i < chars.length(); i++){
d = originalD;
if(d>=0 &&( chars.length()-1 - i ) < d){
d = (d-1) - (chars.length() - 1 - i);
}else if(d < 0 && (chars.length()-1)-((chars.length()-1)-i) < d*(-1)){
d = d+chars.length();
}
else{
d = i+d;
}
if(c == chars.charAt(i)){
return chars.charAt(d);
}
}
return '\0';
}
public static void main(String[] args) {
new Problema23().start(args);
}
}