Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created June 27, 2021 21:59
Show Gist options
  • Select an option

  • Save DreamVB/3428503de40b0ed9b0550d4b4197bb8e to your computer and use it in GitHub Desktop.

Select an option

Save DreamVB/3428503de40b0ed9b0550d4b4197bb8e to your computer and use it in GitHub Desktop.
Morse Code Translator v1.0
// Morse Code Translator v1.0
// Ben a.k.a By DreamVB
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Mose code map
string mose_code = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- "
".-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.. "
"----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.";
//Alphanum map
string alphanum = "A B C D E F G H I J K L M N O P Q R S T U V W X"
" Y Z 0 1 2 3 4 5 6 7 8 9";
std::vector<string>m_code;
std::vector<string>m_alpha;
std::vector<string>string_to_vector(std::string source, unsigned char seperator = ' '){
//Split a string using a seperator into a vector
std::string line;
std::string src = source;
std::vector<string>temp;
size_t x = 0;
//Append seperator is last char is no seperator
if (src[src.length()] != seperator){
src += seperator;
}
//Loop the string to the end
while (x < src.length()){
//Check for seperator
if (src[x] == seperator){
//Push line onto vector
temp.push_back(line);
//Clear line
line.clear();
}
else{
//Keep appending chars to the line string
line += src[x];
}
x++;
}
return temp;
}
//Look up a char in the alphanum vector
size_t lookup_alpha(std::vector<string>vec, unsigned char c){
size_t x = 0;
size_t idx = -1;
//While we are not at the end of the vector loop
while (x < vec.size()){
//Compare char in vector to c
if (vec[x][0] == c){
//Set index
idx = x;
break;
}
//INC counter
x++;
}
//Return index
return idx;
}
size_t lookup_morse(std::vector<string>vec, std::string s){
//Look up a morse code string in the m_code vector
size_t x = 0;
size_t idx = -1;
//While we are not at the end of the vector loop
while (x < vec.size()){
//Compare vec element to s
if (vec[x] == s){
//Set index found
idx = x;
break;
}
//INC Counter
x++;
}
//Return index
return idx;
}
std::string get_morse_code(std::string message){
unsigned char ch = '\0';
int lookup_char = -1;
std::string zbuffer;
//Convert a string of text into encoded morse code
for (auto c : message){
//Get each char as uppercase
ch = toupper(c);
//Look up char
lookup_char = lookup_alpha(m_alpha, ch);
//Check if the char was found in the vector
if (lookup_char != -1){
//Add the morse code
zbuffer += m_code[lookup_char] + " ";
}
else{
//Check for space
if (ch == ' ')
//Append morse code space seperator
zbuffer += "/ ";
}
}
return zbuffer;
}
std::string decode_morse_code(std::string source){
//Split the string into a vector
std::vector<string>v = string_to_vector(source);
std::string zbuffer;
int lookup_str = -1;
for (auto s : v){
//Check for space seperator
if (s == "/"){
//Sppend char #32
zbuffer += " ";
}
else{
//Look up morse code string
lookup_str = lookup_morse(m_code, s);
if (lookup_str != -1)
//Append alpha from the found index
zbuffer += m_alpha[lookup_str];
}
}
//Clear vector
v.clear();
//Return the string
return zbuffer;
}
void init(void){
//Convert the strings to vectors
m_code = string_to_vector(mose_code);
m_alpha = string_to_vector(alphanum);
}
int main(){
std::string text;
//Init
init();
//Encode the string to morse code and print to the console.
text = get_morse_code("I Love Programming On Github What About You ");
std::cout << "Encoded Message" << std::endl;
std::cout << text << std::endl;
std::cout << "Decodeed Message Above" << std::endl;
std::cout << decode_morse_code(text) << std::endl;
std::cout << std::endl << decode_morse_code(".--. .-.. . .- ... . / ..-. --- .-.. .-.. --- .-- "
"/ -- . / .. ..-. / -.-- --- ..- / .-.. .. -.- . / -- -.-- / -.-. --- -.. .");
std::cout << std::endl;
//Clear string and vectors
m_alpha.clear();
m_code.clear();
text.clear();
alphanum.clear();
mose_code.clear();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment