[Solved] Read each morse code from a string line [closed]


You could store the morse codes and their equivalent values in a map,
split the morse string on spaces, loop over these elements and retrieve the resulting values from your map (and concatenate them together) for your final result

this is an example showing the decoded SOS ‘… — …’

#include <string>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>


using namespace std;

vector<string> SplitString (string aString);
vector<string> SplitString (string aString){
  vector<string> vec;
  char * cstr, *p;
  string str = aString;

  cstr = new char [str.size()+1];
  strcpy (cstr, str.c_str());
  p=strtok (cstr," ");

  while (p!=NULL){
   vec.push_back(p);
   p = strtok(NULL," ");
  }

  delete[] cstr;
  return vec;
}

int main(){

  map<string,string> m;
  m["..."] = "S";
  m["---"] = "0";


  vector<string> v;
  string sentence = "... --- ...";
  v = SplitString(sentence);


  vector<string>::iterator it;

  cout << "Our morse (" << sentence << ") decoded to: ";
  for ( it = v.begin() ; it < v.end(); it++ ){
    cout << m[*it];
  }

  cout << endl;

  return 0;
}

you can fill in the other morse codes, would’ve taken me too much time, sorry, used SOS since it’s so well known. 🙂
This example is also probably not very good nor optimised nor recommended, it’s been years since I came near c++. Hope it sparks some better idea in you though.

1

solved Read each morse code from a string line [closed]