-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse
More file actions
executable file
·38 lines (36 loc) · 1.67 KB
/
morse
File metadata and controls
executable file
·38 lines (36 loc) · 1.67 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
#!/usr/bin/java --source 25
void main(String... args) {
var input = String.join(" ", args).trim();
if (input.isEmpty()) {
IO.println("");
return;
}
IO.println(toMorse(input));
}
static String toMorse(String text) {
var sb = new StringBuilder();
var first = true;
for (char c : text.toUpperCase().toCharArray()) {
String code = switch (c) {
case 'A' -> ".-"; case 'B' -> "-..."; case 'C' -> "-.-."; case 'D' -> "-..";
case 'E' -> "."; case 'F' -> "..-."; case 'G' -> "--."; case 'H' -> "....";
case 'I' -> ".."; case 'J' -> ".---"; case 'K' -> "-.-"; case 'L' -> ".-..";
case 'M' -> "--"; case 'N' -> "-."; case 'O' -> "---"; case 'P' -> ".--.";
case 'Q' -> "--.-"; case 'R' -> ".-."; case 'S' -> "..."; case 'T' -> "-";
case 'U' -> "..-"; case 'V' -> "...-"; case 'W' -> ".--"; case 'X' -> "-..-";
case 'Y' -> "-.--"; case 'Z' -> "--..";
case '0' -> "-----"; case '1' -> ".----"; case '2' -> "..---"; case '3' -> "...--";
case '4' -> "....-"; case '5' -> "....."; case '6' -> "-...."; case '7' -> "--...";
case '8' -> "---.."; case '9' -> "----.";
case ' ' -> "/";
case '.' -> ".-.-.-"; case ',' -> "--..--"; case '?' -> "..--.."; case '!' -> "-.-.--";
case ':' -> "---..."; case ';' -> "-.-.-."; case '(' -> "-.--."; case ')' -> "-.--.-";
case '"' -> ".-..-."; case '\'' -> ".----."; case '@' -> ".--.-."; case '&' -> ".-...";
default -> "?";
};
if (!first) sb.append(' ');
sb.append(code);
first = false;
}
return sb.toString();
}