C++ program to convert string to Morse Code.

// ? 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {
  const char letters[37] = { ' ', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
  const string morseLetters[37] = { "    ", ". ___", "___ . . .", "___ . ___ .", "___ . .", ".", ". . ___ .", "___ ___ .", ". . . .", ". .", ". ___ ___ ___", "___ . ___", ". ___ . .",  "___ ___", "___ .", "___ ___ ___", ". ___ ___ .", "___ ___ . ___", ". ___ .", ". . .", "_", ". . ___", ". . . ___", ". ___ ___", "___ . . ___", "___ . ___ ___", "___ ___ . .", ". ___ ___ ___ ___", ". . ___ ___ ___", ". . . ___ ___", ". . . . ___", ". . . . .", "___ . . . .", "___ ___ . . .", "___ ___ ___ . .", "___ ___ ___ ___ .", "___ ___ ___ ___ ___"};

  string textToChange = "";
  string newText = "";

  cout << "Enter text you want to change to Morse code" << endl;
  getline(cin, textToChange);

  transform(textToChange.begin(), textToChange.end(), textToChange.begin(), ::tolower);
  
  for (unsigned int i = 0; i < textToChange.size(); i++) {
    for (unsigned short j = 0; j < 37; j++) {
      if (textToChange[i] == letters[j]) {
        newText += morseLetters[j];
        newText += "   ";

        break;
      }		
    }
  }
  
  cout << "Text in Morse code" << endl << newText;

  int a;
  cin >> a;

  return 0;
}

 



C++ String To Morse Code
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close