| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Basic4GL Morse Code Converter

Page history last edited by matthew 8 years, 5 months ago
  1.  
  2. ' Morse Code Converter
  3. ' Converts ordinary text into Morse code
  4. '
  5.  
  6. ' Variables that we'll be using
  7. '
  8. dim i, stringLength, userInput$
  9. dim currChar$, tempChar$, currMorseChar$
  10. dim alphaData$(37), morseData$(37)
  11.  
  12. ' Data statements for letters and their Morse representation
  13. '
  14. data a, ".-",   b, "-...", c, "-.-.", d, "-..",  e, ".",    f, "..-."
  15. data g, "--.",  h, "....", i, "..",   j, ".---", k, "-.-",  l, ".-.."
  16. data m, "--",   n, "-.",   o, "---",  p, ".--.", q, "--.-", r, ".-."
  17. data s, "...",  t, "-",    u, "..-",  v, "...-", w, ".--",  x, "-..-"
  18. data y, "-.--", z, "--.."
  19.  
  20. ' Data statements for numbers and their Morse representation. Don't
  21. ' forget the empty strings at the end, these are important, for
  22. ' example if the user enters several words separated by a space.
  23. '
  24. data "0", "-----", "1", ".----", "2", "..---", "3", "...--"
  25. data "4", "....-", "5", ".....", "6", "-....", "7", "--..."
  26. data "8", "---..", "9", "----."
  27. data " ", " "
  28.  
  29. ' Ask the user to input something
  30. '
  31. printr "Enter some text please"
  32.  
  33. ' Store the inputted text
  34. '
  35. input userInput$
  36.  
  37. ' No need for upper-case letters
  38. '
  39. userInput$ = lcase$(userInput$)
  40.  
  41. ' Get the length of the users text
  42. '
  43. stringLength = len(userInput$)
  44.  
  45. ' Clear the screen
  46. '
  47. Cls
  48.  
  49. ' Loop through the text
  50. '
  51. for i = 1 to stringLength
  52.  
  53.     ' Get the next letter from the text
  54.     '
  55.     currChar$ = mid$(userInput$, i, 1)
  56.  
  57.     ' Start reading the data statements from the beginning
  58.     '
  59.     reset
  60.    
  61.     ' Set tempChar to an empty string. Without this if the user
  62.     ' enters some text which contains repeating letters or numbers
  63.     ' such as rabbit, speed, 999, 911 etc, the code for the repeated
  64.     ' letter/number will be omitted.
  65.     '
  66.     tempChar$ = " "
  67.  
  68.     ' Loop through the data statements until we find the letter/number
  69.     ' we're trying to convert
  70.     '
  71.     while currChar$ <> tempChar$
  72.         read alphaData$(i), morseData$(i)
  73.         tempChar$ = alphaData$(i)
  74.     wend
  75.    
  76.     ' This is completely unnecessary but it just makes more sense to
  77.     ' print from a variable called currMorseChar than morseData ;-)
  78.     '
  79.     currMorseChar$ = morseData$(i)
  80.          
  81.     ' Print the character and its Morse equivalent
  82.     ' separated by an empty string
  83.     '
  84.     printr currChar$ " " currMorseChar$
  85.    
  86.     ' Output the text to the screen
  87.     '
  88.     drawtext()
  89.  
  90. next
  91.  

 

  • An earlier version of this program can also be found here on the Basic4GL Forum
  • More can be found out about Morse code on this Wikipedia page
  • Code Syntax Highlighting by Quick Highlighter

Comments (0)

You don't have permission to comment on this page.