Convert text to Morse Code

From CodeCodex

Implementations[edit]

D[edit]

 import std.stdio, std.string;
 
 string[char] map;
 
 static this() {
     string codes = "--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --...
                     ---.. ----. ..--.. .- -... -.-. -... . ..-. --. .... .. .--- -.-
                     .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..";
     foreach(i, code; codes.split())
         map[",.0123456789?abcdefghijklmnopqrstuvwxyz"[i]] = code;
 }
 
 string toMorse(string txt) {
     string[] parts;
     foreach(c; txt.tolower())
         parts ~= c in map ? map[c] : toString(c);
     return parts.join(" ");
 }
 
 void main() {
     writefln(toMorse("How are you?"));
 }

Erlang[edit]

char_to_morse(Text) ->
    setup_morse_code(),
    string:join(lists:map(fun(C) -> convert_morse(C) end, string:to_lower(Text)), " ").

setup_morse_code() ->
    Chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz",
    Codes = "--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---.. " ++
            "----. ..--.. .- -... -.-. -... . ..-. --. .... .. .--- -.- .-.. -- " ++
            "-. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..",
    setup_morse_code(Chars, string:tokens(Codes, " ")).

setup_morse_code([], _) -> ok;
setup_morse_code([C|T], [Code|Rest]) ->
    put(C, Code),
    setup_morse_code(T, Rest).

convert_morse(C) ->
    case get(C) of
        undefined -> " ";
        Morse     -> Morse
    end.

Java[edit]

private String charToMorse(char c) {
    switch(c) {
        case 'a': case 'A':
            return ".-";
        case 'b': case 'B':
            return "-...";
        case 'c': case 'C':
            return "-.-.";
        case 'd': case 'D':
            return "--..";
        case 'e': case 'E':
            return ".";
        case 'f': case 'F':
            return "..-.";
        case 'g': case 'G':
            return "--.";
        case 'h': case 'H':
            return "....";
        case 'i': case 'I':
            return "..";
        case 'j': case 'J':
            return ".---";
        case 'k': case 'K':
            return "-.-";
        case 'l': case 'L':
            return ".-..";
        case 'm': case 'M':
            return "--";
        case 'n': case 'N':
            return "-.";
        case 'o': case 'O':
            return "---";
        case 'p': case 'P':
            return ".--.";
        case 'q': case 'Q':
            return "--.-";
        case 'r': case 'R':
            return ".-.";
        case 's': case 'S':
            return "...";
        case 't': case 'T':
            return "-";
        case 'u': case 'U':
            return "..-";
        case 'v': case 'V':
            return "...-";
        case 'w': case 'W':
            return ".--";
        case 'x': case 'X':
            return "-..-";
        case 'y': case 'Y':
            return "-.--";
        case 'z': case 'Z':
            return "--..";
        case '0':
            return "-----";
        case '1':
            return ".----";
        case '2':
            return "..---";
        case '3':
            return "...--";
        case '4':
            return "....-";
        case '5':
            return ".....";
        case '6':
            return "-....";
        case '7':
            return "--...";
        case '8':
            return "---..";
        case '9':
            return "----.";
        case '.':
            return ".-.-.-";
        case '?':
            return "..--..";
        case ',':
            return "--..--";
        case ' ':
            return " ";
        default:
            return null;
    }

}

OCaml[edit]

# let morse = function
    | 'a' -> ".-" | 'b' -> "-..." | 'c' -> "-.-." | 'd' -> "-..." | 'e' -> "."
    | 'f' -> "..-." | 'g' -> "--." | 'h' -> "...." | 'i' -> ".." | 'j' -> ".---"
    | 'k' -> "-.-" | 'l' -> ".-.." | 'm' -> "--" | 'n' -> "-." | 'o' -> "---"
    | 'p' -> ".--." | 'q' -> "--.-" | 'r' -> ".-." | 's' -> "..." | 't' -> "-"
    | 'u' -> "..-" | 'v' -> "...-" | 'w' -> ".--" | 'x' -> "-..-" | 'y' -> "-.--"
    | 'z' -> "--.." | '0' -> "-----" | '1' -> ".----" | '2' -> "..---"
    | '3' -> "...--" | '4' -> "....-" | '5' -> "....." | '6' -> "-...."
    | '7' -> "--..." | '8' -> "---.." | '9' -> "----." | '.' -> ".-.-.-"
    | '?' -> "..--.." | ',' -> "--..--" | ' ' -> " " | _ -> "";;
val morse : char -> string = <fun>

For example:

# String.iter (fun c -> print_string (morse c)) "Hello world";;
..-...-..--- .-----.-..-..-...

Perl[edit]

use Convert::Morse qw(as_morse);
as_morse $str;

PHP[edit]

function toMorse($c){
   switch($c) {
        case 'a': case 'A':
            return ".-";
        case 'b': case 'B':
            return "-...";
        case 'c': case 'C':
            return "-.-.";
        case 'd': case 'D':
            return "-...";
        case 'e': case 'E':
            return ".";
        case 'f': case 'F':
            return "..-.";
        case 'g': case 'G':
            return "--.";
        case 'h': case 'H':
            return "....";
        case 'i': case 'I':
            return "..";
        case 'j': case 'J':
            return ".---";
        case 'k': case 'K':
            return "-.-";
        case 'l': case 'L':
            return ".-..";
        case 'm': case 'M':
            return "--";
        case 'n': case 'N':
            return "-.";
        case 'o': case 'O':
            return "---";
        case 'p': case 'P':
            return ".--.";
        case 'q': case 'Q':
            return "--.-";
        case 'r': case 'R':
            return ".-.";
        case 's': case 'S':
            return "...";
        case 't': case 'T':
            return "-";
        case 'u': case 'U':
            return "..-";
        case 'v': case 'V':
            return "...-";
        case 'w': case 'W':
            return ".--";
        case 'x': case 'X':
            return "-..-";
        case 'y': case 'Y':
            return "-.--";
        case 'z': case 'Z':
            return "--..";
        case '0':
            return "-----";
        case '1':
            return ".----";
        case '2':
            return "..---";
        case '3':
            return "...--";
        case '4':
            return "....-";
        case '5':
            return ".....";
        case '6':
            return "-....";
        case '7':
            return "--...";
        case '8':
            return "---..";
        case '9':
            return "----.";
        case '.':
            return ".-.-.-";
        case '?':
            return "..--..";
        case ',':
            return "--..--";
        case ' ':
            return " ";
        default:
            return "";
    }
}

Python[edit]

chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"
codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---..
           ----. ..--.. .- -... -.-. -... . ..-. --. .... .. .--- -.- .-.. --
           -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."""
keys = dict(zip(chars, codes.split()))

def char2morse(char):
    return keys.get(char.lower(), char)

print ' '.join(char2morse(c) for c in 'SOS')

Ruby[edit]

chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"
codes = %w(--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---..
           ----. ..--.. .- -... -.-. -... . ..-. --. .... .. .--- -.- .-.. --
           -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..)

MORSE = Hash[chars.split("").zip(codes)]
MORSE.default=" "

def char2morse(chars)
  chars.downcase.split("").map{|c|MORSE[c]}.join(" ")
end

p char2morse("How are you?")

Seed7[edit]

const func string: charToMorse (in char: ch) is func
  result
    var string: result is "";
  begin
    case ch of
      when {'a', 'A'}: result := ".-";
      when {'b', 'B'}: result := "-...";
      when {'c', 'C'}: result := "-.-.";
      when {'d', 'D'}: result := "-..";
      when {'e', 'E'}: result := ".";
      when {'f', 'F'}: result := "..-.";
      when {'g', 'G'}: result := "--.";
      when {'h', 'H'}: result := "....";
      when {'i', 'I'}: result := "..";
      when {'j', 'J'}: result := ".---";
      when {'k', 'K'}: result := "-.-";
      when {'l', 'L'}: result := ".-..";
      when {'m', 'M'}: result := "--";
      when {'n', 'N'}: result := "-.";
      when {'o', 'O'}: result := "---";
      when {'p', 'P'}: result := ".--.";
      when {'q', 'Q'}: result := "--.-";
      when {'r', 'R'}: result := ".-.";
      when {'s', 'S'}: result := "...";
      when {'t', 'T'}: result := "-";
      when {'u', 'U'}: result := "..-";
      when {'v', 'V'}: result := "...-";
      when {'w', 'W'}: result := ".--";
      when {'x', 'X'}: result := "-..-";
      when {'y', 'Y'}: result := "-.--";
      when {'z', 'Z'}: result := "--..";
      when {'0'}:      result := "-----";
      when {'1'}:      result := ".----";
      when {'2'}:      result := "..---";
      when {'3'}:      result := "...--";
      when {'4'}:      result := "....-";
      when {'5'}:      result := ".....";
      when {'6'}:      result := "-....";
      when {'7'}:      result := "--...";
      when {'8'}:      result := "---..";
      when {'9'}:      result := "----.";
      when {'!'}:      result := "-.-.--";
      when {'"'}:      result := ".-..-.";
      when {'$'}:      result := "...-..-";
      when {'''}:      result := ".----.";
      when {'('}:      result := "-.--.";
      when {')'}:      result := "-.--.-";
      when {'+'}:      result := ".-.-.";
      when {','}:      result := "--..--";
      when {'-'}:      result := "-....-";
      when {'.'}:      result := ".-.-.-";
      when {'/'}:      result := "-..-.";
      when {':'}:      result := "---...";
      when {';'}:      result := "-.-.-.";
      when {'='}:      result := "-...-";
      when {'?'}:      result := "..--..";
      when {'@'}:      result := ".--.-.";
      when {' '}:      result := " ";
    end case;
  end func;

const func string: stringToMorse (in string: stri) is func
  result
    var string: result is "";
  local
    var char: ch is ' ';
  begin
    for ch range stri do
      result &:= charToMorse(ch) & " ";
    end for;
  end func;