-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMorseCode.java
More file actions
30 lines (28 loc) · 1.12 KB
/
MorseCode.java
File metadata and controls
30 lines (28 loc) · 1.12 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
class MorseCode {
final static String[] morseCode = {".-","-...","-.-.","-..",".","..-.","--.","...."
,"..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","..."
,"-","..-","...-",".--","-..-","-.--","--.."};
final static String al = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String convertStringToMorseCode(String message) {
String s = "";
for (int i = 0; i < message.length(); i++) {
s += morseCode[al.indexOf(message.charAt(i))] + " ";
}
return s;
}
public static String morseToString(String morse) {
String s = "";
String[] morseArray = morse.split(" ");
for (int i = 0; i < morseArray.length; i++) {
for (int k = 0; k < morseCode.length; k++) {
if (morseArray[i].equals(morseCode[k]))
s += al.charAt(k);
}
}
return s;
}
public static void main(String[] args) {
System.out.println(convertStringToMorseCode("CANTEEN"));
System.out.println(morseToString("-.-. .- -. - . . -."));
}
}