diff --git a/morse/morse_code.cc b/morse/morse_code.cc index 5c91d32..36ec13a 100644 --- a/morse/morse_code.cc +++ b/morse/morse_code.cc @@ -16,7 +16,7 @@ std::string Morse_code::encode(const std::string &str) { std::string res; for (auto &c : str) { - auto it = morse_map.find(c); + auto it = morse_map.find(toupper(c)); // convert if (it != morse_map.end()) { res += it->second + " "; } @@ -24,6 +24,22 @@ std::string Morse_code::encode(const std::string &str) { return res; } -// std::string Morse_code::decode(const std::string &str) { +std::string Morse_code::decode(std::string str) { + std::stringstream in(str); + std::string res; -// } \ No newline at end of file + std::string s; + while(in >> s) { + + auto it = std::find_if(morse_map.begin(), morse_map.end(), [&](std::pair &p) { + return p.second == s; + }); + + if (it != morse_map.end()) { + res += it->first; + } else { + res += '?'; + } + } + return res; +} \ No newline at end of file diff --git a/morse/morse_code.h b/morse/morse_code.h index 0ea2e56..d0a149b 100644 --- a/morse/morse_code.h +++ b/morse/morse_code.h @@ -2,13 +2,16 @@ #include #include #include +#include +#include +#include class Morse_code { public: Morse_code(std::string filename); std::string encode(const std::string &str); - std::string decode(const std::string &str); + std::string decode(std::string str); private: std::map morse_map; }; \ No newline at end of file