Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
morse_code.cpp File Reference

Implementation of [Morse Code] (https://en.wikipedia.org/wiki/Morse_code). More...

#include <cassert>
#include <iostream>
#include <string>
#include <vector>
Include dependency graph for morse_code.cpp:

Namespaces

namespace  ciphers
 Algorithms for encryption and decryption.
 
namespace  morse
 Functions for [Morse Code] (https://en.wikipedia.org/wiki/Morse_code).
 

Functions

std::string ciphers::morse::char_to_morse (const char &c)
 
char ciphers::morse::morse_to_char (const std::string &s)
 
std::string ciphers::morse::encrypt (const std::string &text)
 
std::string ciphers::morse::decrypt (const std::string &text)
 
static void test ()
 Function to test above algorithm.
 
int main ()
 Main function.
 

Detailed Description

Implementation of [Morse Code] (https://en.wikipedia.org/wiki/Morse_code).

Author
Deep Raval

Morse code is a method used in telecommunication to encode text characters as standardized sequences of two different signal durations, called dots and dashes or dits and dahs. Morse code is named after Samuel Morse, an inventor of the telegraph.

Function Documentation

◆ char_to_morse()

std::string ciphers::morse::char_to_morse ( const char & c)

Get the morse representation for given character.

Parameters
cCharacter
Returns
morse representation string of character
33 {
34 // return corresponding morse code
35 switch (c) {
36 case 'a':
37 return ".-";
38 case 'b':
39 return "-...";
40 case 'c':
41 return "-.-.";
42 case 'd':
43 return "-..";
44 case 'e':
45 return ".";
46 case 'f':
47 return "..-.";
48 case 'g':
49 return "--.";
50 case 'h':
51 return "....";
52 case 'i':
53 return "..";
54 case 'j':
55 return ".---";
56 case 'k':
57 return "-.-";
58 case 'l':
59 return ".-..";
60 case 'm':
61 return "--";
62 case 'n':
63 return "-.";
64 case 'o':
65 return "---";
66 case 'p':
67 return ".--.";
68 case 'q':
69 return "--.-";
70 case 'r':
71 return ".-.";
72 case 's':
73 return "...";
74 case 't':
75 return "-";
76 case 'u':
77 return "..-";
78 case 'v':
79 return "...-";
80 case 'w':
81 return ".--";
82 case 'x':
83 return "-..-";
84 case 'y':
85 return "-.--";
86 case 'z':
87 return "--..";
88 case '1':
89 return ".----";
90 case '2':
91 return "..---";
92 case '3':
93 return "...--";
94 case '4':
95 return "....-";
96 case '5':
97 return ".....";
98 case '6':
99 return "-....";
100 case '7':
101 return "--...";
102 case '8':
103 return "---..";
104 case '9':
105 return "----.";
106 case '0':
107 return "-----";
108 default:
109 std::cerr << "Found invalid character: " << c << ' ' << std::endl;
110 std::exit(0);
111 }
112}
T endl(T... args)
T exit(T... args)
Here is the call graph for this function:

◆ decrypt()

std::string ciphers::morse::decrypt ( const std::string & text)

Decrypt given morse coded text.

Parameters
texttext to be decrypted
Returns
new decrypted text
216 {
217 // Going through each character of text and converting it
218 // back to normal representation.
219 std::string decrypted_text = ""; // Empty string to store decrypted text
220 // Spliting string (with delimiter = " ") and storing it
221 // in vector
222 std::size_t pos_start = 0, pos_end = 0, delim_len = 1;
224 while ((pos_end = text.find(' ', pos_start)) != std::string::npos) {
225 std::string token = text.substr(pos_start, pos_end - pos_start);
226 pos_start = pos_end + delim_len;
227 splits.push_back(token);
228 }
229
230 // Traversing through each morse code string
231 for (const std::string &s : splits) {
232 // Add corresponding character
233 decrypted_text += ciphers::morse::morse_to_char(s);
234 }
235
236 return decrypted_text; // Returning decrypted text
237}
T find(T... args)
char morse_to_char(const std::string &s)
Definition morse_code.cpp:118
T push_back(T... args)
T substr(T... args)
Here is the call graph for this function:

◆ encrypt()

std::string ciphers::morse::encrypt ( const std::string & text)

Encrypt given text using morse code.

Parameters
texttext to be encrypted
Returns
new encrypted text
202 {
203 std::string encrypted_text = ""; // Empty string to store encrypted text
204 // Going through each character of text and converting it
205 // to morse representation
206 for (const char &c : text) {
207 encrypted_text += ciphers::morse::char_to_morse(c) + " ";
208 }
209 return encrypted_text; // Returning encrypted text
210}
std::string char_to_morse(const char &c)
Definition morse_code.cpp:33
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
268 {
269 // Testing
270 test();
271 return 0;
272}
static void test()
Function to test above algorithm.
Definition morse_code.cpp:245
Here is the call graph for this function:

◆ morse_to_char()

char ciphers::morse::morse_to_char ( const std::string & s)

Get character from the morse representation.

Parameters
sMorse representation
Returns
corresponding character
118 {
119 // return corresponding character
120 if (s == ".-") {
121 return 'a';
122 } else if (s == "-...") {
123 return 'b';
124 } else if (s == "-.-.") {
125 return 'c';
126 } else if (s == "-..") {
127 return 'd';
128 } else if (s == ".") {
129 return 'e';
130 } else if (s == "..-.") {
131 return 'f';
132 } else if (s == "--.") {
133 return 'g';
134 } else if (s == "....") {
135 return 'h';
136 } else if (s == "..") {
137 return 'i';
138 } else if (s == ".---") {
139 return 'j';
140 } else if (s == "-.-") {
141 return 'k';
142 } else if (s == ".-..") {
143 return 'l';
144 } else if (s == "--") {
145 return 'm';
146 } else if (s == "-.") {
147 return 'n';
148 } else if (s == "---") {
149 return 'o';
150 } else if (s == ".--.") {
151 return 'p';
152 } else if (s == "--.-") {
153 return 'q';
154 } else if (s == ".-.") {
155 return 'r';
156 } else if (s == "...") {
157 return 's';
158 } else if (s == "-") {
159 return 't';
160 } else if (s == "..-") {
161 return 'u';
162 } else if (s == "...-") {
163 return 'v';
164 } else if (s == ".--") {
165 return 'w';
166 } else if (s == "-..-") {
167 return 'x';
168 } else if (s == "-.--") {
169 return 'y';
170 } else if (s == "--..") {
171 return 'z';
172 } else if (s == ".----") {
173 return '1';
174 } else if (s == "..---") {
175 return '2';
176 } else if (s == "...--") {
177 return '3';
178 } else if (s == "....-") {
179 return '4';
180 } else if (s == ".....") {
181 return '5';
182 } else if (s == "-....") {
183 return '6';
184 } else if (s == "--...") {
185 return '7';
186 } else if (s == "---..") {
187 return '8';
188 } else if (s == "----.") {
189 return '9';
190 } else if (s == "-----") {
191 return '0';
192 } else {
193 std::cerr << "Found invalid Morse code: " << s << ' ' << std::endl;
194 std::exit(0);
195 }
196}
Here is the call graph for this function:

◆ test()

static void test ( )
static

Function to test above algorithm.

Returns
void
245 {
246 // Test 1
247 std::string text1 = "01234567890";
248 std::string encrypted1 = ciphers::morse::encrypt(text1);
249 std::string decrypted1 = ciphers::morse::decrypt(encrypted1);
250 assert(text1 == decrypted1);
251 std::cout << "Original text : " << text1 << std::endl;
252 std::cout << "Encrypted text : " << encrypted1 << std::endl;
253 std::cout << "Decrypted text : " << decrypted1 << std::endl;
254 // Test 2
255 std::string text2 = "abcdefghijklmnopqrstuvwxyz";
256 std::string encrypted2 = ciphers::morse::encrypt(text2);
257 std::string decrypted2 = ciphers::morse::decrypt(encrypted2);
258 assert(text2 == decrypted2);
259 std::cout << "Original text : " << text2 << std::endl;
260 std::cout << "Encrypted text : " << encrypted2 << std::endl;
261 std::cout << "Decrypted text : " << decrypted2 << std::endl;
262}
std::string encrypt(const std::string &text)
Definition morse_code.cpp:202
std::string decrypt(const std::string &text)
Definition morse_code.cpp:216
Here is the call graph for this function: