moorse_string[] = {".-", // 0 = A ('A' = ASCII code 65; 'a' = ASCII code 97)
"-...", // 1 = B
"-.-.", // 2 = C
"-..", // 3 = D
".", // 4 = E
"..-.", // 5 = F
"--.", // 6 = G
"....", // 7 = H
"..", // 8 = I
".---", // 9 = J
"-.-", // 10 = K
".-..", // 11 = L
"--", // 12 = M
"-.", // 13 = N
"---", // 14 = O
".--.", // 15 = P
"--.-", // 16 = Q
".-.", // 17 = R
"...", // 18 = S
"-", // 19 = T
"..-", // 20 = U
"...-", // 21 = V
".--", // 22 = W
"-..-", // 23 = X
"-.--", // 24 = Y
"--..", // 25 = Z
"-----", // 26 = 0 (ASCII code 48)
".----", // 27 = 1
"..---", // 28 = 2
"...--", // 29 = 3
"....-", // 30 = 4
".....", // 31 = 5
"-....", // 32 = 6
"--...", // 33 = 7
"---..", // 33 = 8
"----.", // 34 = 9
"--..--", // 36 = , (ASCII code 44)
".-.-.-" // 37 = . (ASCII code 46)
}
Only thing I can think of is to match each string of code with the array of strings above until a match is found. I want to think of a better way to decode this without brute force. For example, if I have "..---". This is located at 28 index of the array. and My code now looks from 0 - 28 and finds a match and breaks. Please suggest better ideas thanks.
I want to handle following list of chars only:
and Morse encodings within a word are separated by a single space. Spaces between words and new lines are separated by three spaces.
Answer does not have to be code, but codes are accepted. Answer can be any valid suggestions that explain the logic wholly in words(atleast). I just want to explore different way of thinking. Any answer that is different from what I suggested will be accepted even if it is a worse algorithm. But if there is 2 answers then the more elegant one will be accepted. I am not asking for the solution but ideas. How would you approach the problem? and why? What is the logic that drives the algorithm? If you consider you way better than what I suggested then why?