Implementación del Código Morse

El código Morse es un método para transmitir información de texto como una serie de tonos, luces o clics que pueden ser entendidos directamente por un oyente u observador experto sin equipo especial. Lleva el nombre de Samuel FB Morse, un inventor del telégrafo.
El algoritmo es muy simple. Cada carácter en el idioma inglés se sustituye por una serie de ‘puntos’ y ‘guiones’ o, a veces, simplemente ‘punto’ o ‘guión’ singular y viceversa.
Cada string de texto se convierte en la serie de puntos y guiones. Para ello, cada carácter se convierte en su código Morse y se adjunta en un mensaje codificado. Aquí hemos copiado el espacio tal como es. Hemos considerado tanto números como alfabetos.
 

Ejemplos: 

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

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

C++

// 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 "-.--";
    case 'z':
        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 '0':
        return "-----";
    default:
        cerr << "Found invalid character: " << x << ' '
             << std::endl;
        exit(0);
    }
}
 
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;
}

Java

// 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 "--..";
            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 '0':
                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.

Python 3

# 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 "--.."
    elif x is '1':
        return ".----";
    elif x is '2':
        return "..---";
    elif x is '3':
        return "...--";
    elif x is '4':
        return "....-";
    elif x is '5':
        return ".....";
    elif x is '6':
        return "-....";
    elif x is '7':
        return "--...";
    elif x is '8':
        return "---..";
    elif x is '9':
        return "----.";
    elif x is '0':
        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

C#

// 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 "--..";
             
            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 '0':
                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.

PHP

<?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 "-.--";
    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 '0':
     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
?>

Producción: 
 

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

Publicación traducida automáticamente

Artículo escrito por shubham_rana_77 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *