Morse Code Implementation
Last Updated :
18 Apr, 2023
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. We have considered both numbers and alphabets.

Examples:
Input : geeksforgeeks
Output : --...-.-.....-.---.-.--...-.-...
Input : program
Output : .--..-.-----..-..---
Implementation:
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
?>
JavaScript
// JavaScript program to demonstrate Morse code
// function to encode a character as Morse code
function morseEncode(x) {
// refer to the Morse table
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:
console.error("Found invalid character: " + x);
process.exit(0);
}
}
// function to print Morse code
function morseCode(s) {
// character by character print Morse code
let result = "";
for (let i = 0; i < s.length; i++) {
result += morseEncode(s[i]);
}
console.log(result);
}
// Driver's code
let s = "geeksforgeeks";
morseCode(s);
// This code is contributed by rudra1807raj
Output--...-.-.....-.---.-.--...-.-...
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read