Difference between revisions of "Convert text to Morse Code"

From CodeCodex

m
Line 103: Line 103:
 
     | '?' -> "..--.." | ',' -> "--..--" | ' ' -> " " | _ -> "";;
 
     | '?' -> "..--.." | ',' -> "--..--" | ' ' -> " " | _ -> "";;
 
val morse : char -> string = <fun>
 
val morse : char -> string = <fun>
 +
</pre>
 +
For example:
 +
<pre>
 +
# String.iter (fun c -> print_string (morse c)) "Hello world";;
 +
..-...-..--- .-----.-..-..-...
 
</pre>
 
</pre>
 
===PHP===
 
===PHP===
Line 192: Line 197:
 
     }
 
     }
 
}
 
}
</pre>
 
For example:
 
<pre>
 
# String.iter (fun c -> print_string (morse c)) "Hello world"; print_newline();;
 
..-...-..--- .-----.-..-..-...
 
- : unit = ()
 
 
</pre>
 
</pre>
  

Revision as of 18:50, 13 January 2007

Implementations

Java

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

# 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";;
..-...-..--- .-----.-..-..-...

PHP

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 "";
    }
}