Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions ciphers/morse_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
#include <cassert>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

Expand All @@ -30,7 +31,7 @@ namespace morse {
* @param c Character
* @returns morse representation string of character
*/
std::string char_to_morse(const char &c) {
std::string char_to_morse(const char& c) {
// return corresponding morse code
switch (c) {
case 'a':
Expand Down Expand Up @@ -106,16 +107,15 @@ std::string char_to_morse(const char &c) {
case '0':
return "-----";
default:
std::cerr << "Found invalid character: " << c << ' ' << std::endl;
std::exit(0);
throw std::invalid_argument("Found invalid character");
}
}
/**
* Get character from the morse representation.
* @param s Morse representation
* @returns corresponding character
*/
char morse_to_char(const std::string &s) {
char morse_to_char(const std::string& s) {
// return corresponding character
if (s == ".-") {
return 'a';
Expand Down Expand Up @@ -190,20 +190,19 @@ char morse_to_char(const std::string &s) {
} else if (s == "-----") {
return '0';
} else {
std::cerr << "Found invalid Morse code: " << s << ' ' << std::endl;
std::exit(0);
throw std::invalid_argument("Found invalid Morse code");
}
}
/**
* Encrypt given text using morse code.
* @param text text to be encrypted
* @returns new encrypted text
*/
std::string encrypt(const std::string &text) {
std::string encrypt(const std::string& text) {
std::string encrypted_text = ""; // Empty string to store encrypted text
// Going through each character of text and converting it
// to morse representation
for (const char &c : text) {
for (const char& c : text) {
encrypted_text += ciphers::morse::char_to_morse(c) + " ";
}
return encrypted_text; // Returning encrypted text
Expand All @@ -213,7 +212,7 @@ std::string encrypt(const std::string &text) {
* @param text text to be decrypted
* @returns new decrypted text
*/
std::string decrypt(const std::string &text) {
std::string decrypt(const std::string& text) {
// Going through each character of text and converting it
// back to normal representation.
std::string decrypted_text = ""; // Empty string to store decrypted text
Expand All @@ -228,7 +227,7 @@ std::string decrypt(const std::string &text) {
}

// Traversing through each morse code string
for (const std::string &s : splits) {
for (const std::string& s : splits) {
// Add corresponding character
decrypted_text += ciphers::morse::morse_to_char(s);
}
Expand Down