Guest User

Very basic English-to-Morse converter (C++)

a guest
Sep 24th, 2011
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. /*
  2. * English to Morse converter
  3. * 24/09/2011
  4. * This program takes an English string and converts it to Morse code
  5. */
  6.  
  7. #include <iostream>
  8. #include <map>
  9. #include <string>
  10. #include <cctype>
  11.  
  12. using namespace std;
  13.  
  14. void buildMorseMap();
  15. void EnglishToMorse(string input);
  16. map <std::string, std::string> morseMap;
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.  
  21.     buildMorseMap();
  22.  
  23.     string input;
  24.  
  25.     cout<<"Write one sentence: ";
  26.     getline(cin,input); // Read in a whole line of text from the keyboard - this won't break if you use spaces as well
  27.  
  28.     EnglishToMorse(input);
  29.  
  30.     return (0);
  31. }
  32.  
  33. // This function does the actual conversion
  34. void EnglishToMorse(string input)
  35. {
  36.  
  37.     // Convert the whole string to uppercase because there are no map entries for the lowercase originals
  38.     for (int i=0; i<input.length(); i++)
  39.     {
  40.         input.at(i) = toupper(input.at(i));
  41.     }
  42.  
  43.     string completedMorse; // Temporary string in which to store the completed Morse string for output
  44.  
  45.     for (int i=0; i<input.length(); i++)
  46.     {
  47.         string temp(1, input.at(i)); // Use a temporary string variable for the lookups, because the .at() function returns a primitive char rather than a string
  48.         completedMorse.append(morseMap[temp]);
  49.         completedMorse.append(" "); // Put a space in between each part of the Morse output
  50.     }
  51.  
  52.     cout<<"Morse: "<<completedMorse<<endl;
  53. }
  54.  
  55. // This function builds the Map container for storing the letter->Morse conversions
  56. // This acts as a "lookup" table i.e. you can ask what the morse value for 'A' is and it'll tell you
  57. // Mappings referred to at http://tcfreenet.org/people/calguire/morse.html
  58. void buildMorseMap()
  59. {  
  60.     morseMap.insert(std::make_pair("A",".-"));
  61.     morseMap.insert(std::make_pair("B","-..."));
  62.     morseMap.insert(std::make_pair("C","-.-."));
  63.     morseMap.insert(std::make_pair("D","-.."));
  64.     morseMap.insert(std::make_pair("E","."));
  65.     morseMap.insert(std::make_pair("F","..-."));
  66.     morseMap.insert(std::make_pair("G","--."));
  67.     morseMap.insert(std::make_pair("H","...."));
  68.     morseMap.insert(std::make_pair("I",".."));
  69.     morseMap.insert(std::make_pair("J",".---"));
  70.     morseMap.insert(std::make_pair("K","-.-"));
  71.     morseMap.insert(std::make_pair("L",".-.."));
  72.     morseMap.insert(std::make_pair("M","--"));
  73.     morseMap.insert(std::make_pair("N","-."));
  74.     morseMap.insert(std::make_pair("O","---"));
  75.     morseMap.insert(std::make_pair("P",".--."));
  76.     morseMap.insert(std::make_pair("Q","--.-"));
  77.     morseMap.insert(std::make_pair("R",".-."));
  78.     morseMap.insert(std::make_pair("S","..."));
  79.     morseMap.insert(std::make_pair("T","-"));
  80.     morseMap.insert(std::make_pair("U","..-"));
  81.     morseMap.insert(std::make_pair("V","...-"));
  82.     morseMap.insert(std::make_pair("W",".--"));
  83.     morseMap.insert(std::make_pair("X","-..-"));
  84.     morseMap.insert(std::make_pair("Y","-.--"));
  85.     morseMap.insert(std::make_pair("Z","--.."));
  86.     morseMap.insert(std::make_pair("1",".----"));
  87.     morseMap.insert(std::make_pair("2","..---"));
  88.     morseMap.insert(std::make_pair("3","...--"));
  89.     morseMap.insert(std::make_pair("4","....-"));
  90.     morseMap.insert(std::make_pair("5","....."));
  91.     morseMap.insert(std::make_pair("6","-...."));
  92.     morseMap.insert(std::make_pair("7","--..."));
  93.     morseMap.insert(std::make_pair("8","---.."));
  94.     morseMap.insert(std::make_pair("9","----."));
  95.     morseMap.insert(std::make_pair("0","-----"));
  96.     morseMap.insert(std::make_pair(",","--..--"));
  97.     morseMap.insert(std::make_pair(".",".-.-.-"));
  98.     morseMap.insert(std::make_pair("?","..--.."));
  99.     morseMap.insert(std::make_pair(";","-.-.-"));
  100.     morseMap.insert(std::make_pair(":","---..."));
  101.     morseMap.insert(std::make_pair("/","-..-."));
  102.     morseMap.insert(std::make_pair("-","-....-"));
  103.     morseMap.insert(std::make_pair("()","-.--.-"));
  104.     morseMap.insert(std::make_pair("_","..--.-"));
  105. }
Advertisement
Add Comment
Please, Sign In to add comment