From 4163c9d70f2b2f16470d3339f1632bc4569e11cb Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 26 Feb 2024 15:19:11 +0100 Subject: [PATCH] add a method for single chars --- src/morse.cpp | 26 +++++++++++++++++++++++++- src/morse.h | 3 ++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/morse.cpp b/src/morse.cpp index 8dac9f2..97ff2a2 100644 --- a/src/morse.cpp +++ b/src/morse.cpp @@ -78,8 +78,32 @@ std::string messageToMorse(std::string &msg) // Append word space if last char was not a blank if (msg.back() != ' ') { - morseString.push_back('w'); + //morseString.push_back('w'); } return morseString; +} + +std::string charToMorse(char ch) { + std::string morseString {}; + + if (ch == ' ') { + morseString.push_back('w'); + return morseString; + } + + for (unsigned int j = 0; j < morseCode[ch].length(); j++) { + char m = morseCode[ch][j]; + + morseString += m; + + if (j < morseCode[ch].length() - 1) { + morseString.push_back('i'); + } + } + + //morseString.push_back('c'); + + return morseString; + } \ No newline at end of file diff --git a/src/morse.h b/src/morse.h index 4594f6b..bdb10cd 100644 --- a/src/morse.h +++ b/src/morse.h @@ -2,4 +2,5 @@ #include -std::string messageToMorse(std::string &msg); \ No newline at end of file +std::string messageToMorse(std::string &msg); +std::string charToMorse(char ch); \ No newline at end of file