Morse Code Implementation

Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener or observer without special equipment. It is named for Samuel F. B. Morse, an inventor of the telegraph.

The algorithm is very simple. Every character in the English language is substituted by a series of ‘dots’ and ‘dashes’ or sometimes just singular ‘dot’ or ‘dash’ and vice versa.

Every text string is converted into the series of dots and dashes. For this every character is converted into its Morse code and appended in encoded message. Here we have copied space as it is. And we have not considered numbers, only alphabets.



Examples:

Input :  geeksforgeeks
Output : --...-.-.....-.---.-.--...-.-...

Input :  program
Output : .--..-.-----..-..---

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate Morse code
#include <iostream>
using namespace std;
  
// function to encode a alphabet as
// Morse code
string morseEncode(char x) {
  
  // refer to the Morse table
  // image attached in the article
  switch (x) {
  case 'a':
    return ".-";
  case 'b':
    return "-...";
  case 'c':
    return "-.-.";
  case 'd':
    return "-..";
  case 'e':
    return ".";
  case 'f':
    return "..-.";
  case 'g':
    return "--.";
  case 'h':
    return "....";
  case 'i':
    return "..";
  case 'j':
    return ".---";
  case 'k':
    return "-.-";
  case 'l':
    return ".-..";
  case 'm':
    return "--";
  case 'n':
    return "-.";
  case 'o':
    return "---";
  case 'p':
    return ".--.";
  case 'q':
    return "--.-";
  case 'r':
    return ".-.";
  case 's':
    return "...";
  case 't':
    return "-";
  case 'u':
    return "..-";
  case 'v':
    return "...-";
  case 'w':
    return ".--";
  case 'x':
    return "-..-";
  case 'y':
    return "-.--";
  // for space
  case 'z':
    return "--..";
  }
}
  
void morseCode(string s) {
  
  // character by character print 
  // Morse code
  for (int i = 0; s[i]; i++)
    cout << morseEncode(s[i]);
  cout << endl;
}
  
// Driver's code
int main() {
  string s = "geeksforgeeks";
  morseCode(s);
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate Morse code
class GFG
{
    // function to encode a alphabet as
    // Morse code
    static String morseEncode(char x) 
    {
      
        // refer to the Morse table
        // image attached in the article
        switch (x) 
        {
            case 'a':
                return ".-";
            case 'b':
                return "-...";
            case 'c':
                return "-.-.";
            case 'd':
                return "-..";
            case 'e':
                return ".";
            case 'f':
                return "..-.";
            case 'g':
                return "--.";
            case 'h':
                return "....";
            case 'i':
                return "..";
            case 'j':
                return ".---";
            case 'k':
                return "-.-";
            case 'l':
                return ".-..";
            case 'm':
                return "--";
            case 'n':
                return "-.";
            case 'o':
                return "---";
            case 'p':
                return ".--.";
            case 'q':
                return "--.-";
            case 'r':
                return ".-.";
            case 's':
                return "...";
            case 't':
                return "-";
            case 'u':
                return "..-";
            case 'v':
                return "...-";
            case 'w':
                return ".--";
            case 'x':
                return "-..-";
            case 'y':
                return "-.--";
            // for space
            case 'z':
                return "--..";
        }
        return "";
    }
      
    static void morseCode(String s) 
    {
        // character by character print 
        // Morse code
        for (int i = 0;i<s.length(); i++)
            System.out.print(morseEncode(s.charAt(i)));
            System.out.println();
    }
      
    // Driver code 
    public static void main (String[] args)
    {
        String s = "geeksforgeeks";
        morseCode(s);
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to demonstrate Morse code
  
# function to encode a alphabet as
# Morse code
def morseEncode(x):
      
    # refer to the Morse table
    # image attached in the article
    if x is 'a':
        return ".-"
    elif x is 'b':
        return "-..."
    elif x is 'c':
        return "-.-."
    elif x is 'd':
        return "-.."
    elif x is 'e':
        return "."
    elif x is 'f':
        return "..-."
    elif x is 'g':
        return "--."
    elif x is 'h':
        return "...."
    elif x is 'i':
        return ".."
    elif x is 'j':
        return ".---"
    elif x is 'k':
        return "-.-"
    elif x is 'l':
        return ".-.."
    elif x is 'm':
        return "--"
    elif x is 'n':
        return "-."
    elif x is 'o':
        return "---"
    elif x is 'p':
        return ".--."
    elif x is 'q':
        return "--.-"
    elif x is 'r':
        return ".-."
    elif x is 's':
        return "..."
    elif x is 't':
        return "-"
    elif x is 'u':
        return "..-"
    elif x is 'v':
        return "...-"
    elif x is 'w':
        return ".--"
    elif x is 'x':
        return "-..-"
    elif x is 'y':
        return "-.--"
    elif x is 'z':
        return "--.."
  
# character by character print
# Morse code
def morseCode(s):
    for character in s:
        print(morseEncode(character), end = "")
  
# Driver Code
if __name__ == "__main__":
    s = "geeksforgeeks"
    morseCode(s)
  
# This code is contributed 
# by Vivek Kumar Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate Morse code
using System;
  
class GFG
{
    // function to encode a alphabet as
    // Morse code
    static string morseEncode(char x) 
    {
      
        // refer to the Morse table
        // image attached in the article
        switch (x) 
        {
            case 'a':
                return ".-";
            case 'b':
                return "-...";
            case 'c':
                return "-.-.";
            case 'd':
                return "-..";
            case 'e':
                return ".";
            case 'f':
                return "..-.";
            case 'g':
                return "--.";
            case 'h':
                return "....";
            case 'i':
                return "..";
            case 'j':
                return ".---";
            case 'k':
                return "-.-";
            case 'l':
                return ".-..";
            case 'm':
                return "--";
            case 'n':
                return "-.";
            case 'o':
                return "---";
            case 'p':
                return ".--.";
            case 'q':
                return "--.-";
            case 'r':
                return ".-.";
            case 's':
                return "...";
            case 't':
                return "-";
            case 'u':
                return "..-";
            case 'v':
                return "...-";
            case 'w':
                return ".--";
            case 'x':
                return "-..-";
            case 'y':
                return "-.--";
            // for space
            case 'z':
                return "--..";
        }
        return "";
    }
      
    static void morseCode(string s) 
    {
        // character by character print 
        // Morse code
        for (int i = 0;i<s.Length; i++)
            Console.Write(morseEncode(s[i]));
            Console.WriteLine();
    }
      
    // Driver code 
    public static void Main ()
    {
        string s = "geeksforgeeks";
        morseCode(s);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to demonstrate
// Morse code
  
// function to encode a  
// alphabet as Morse code
function morseEncode($x)
{
  
// refer to the Morse table
// image attached in the article
switch ($x) {
case 'a':
    return ".-";
case 'b':
    return "-...";
case 'c':
    return "-.-.";
case 'd':
    return "-..";
case 'e':
    return ".";
case 'f':
    return "..-.";
case 'g':
    return "--.";
case 'h':
    return "....";
case 'i':
    return "..";
case 'j':
    return ".---";
case 'k':
    return "-.-";
case 'l':
    return ".-..";
case 'm':
    return "--";
case 'n':
    return "-.";
case 'o':
    return "---";
case 'p':
    return ".--.";
case 'q':
    return "--.-";
case 'r':
    return ".-.";
case 's':
    return "...";
case 't':
    return "-";
case 'u':
    return "..-";
case 'v':
    return "...-";
case 'w':
    return ".--";
case 'x':
    return "-..-";
case 'y':
    return "-.--";
      
// for space
case 'z':
    return "--..";
}
}
  
function morseCode($s
{
  
// Character by character 
// print  Morse code
for ($i = 0; $i<strlen($s); $i++)
    echo morseEncode($s[$i]);
echo "\n";
}
  
// Driver code
$s = "geeksforgeeks";
morseCode($s);
  
// This code is contributed by mits 
?>

chevron_right


Output:

--...-.-.....-.---.-.--...-.-...


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.