fork download
  1. //Maxwell Brewer CS1A Chapter 10, p. 591, #17
  2. //
  3. /*******************************************************************************
  4.  * TEXT TO MORSE CODE
  5.  * _____________________________________________________________________________
  6.  * This program takes a user-provided text string and converts it into
  7.  * Morse code. Each character is translated to its corresponding Morse code
  8.  * representation, based on a predefined set of mappings for letters, digits,
  9.  * and common punctuation. Unsupported characters are flagged as invalid.
  10.  *
  11.  * INPUT:
  12.  * inputString : (maximum 100 characters) containing
  13.  * : letters, numbers, spaces, and common
  14.  * : punctuation marks (e.g., '.', ',', '?').
  15.  *
  16.  * OUTPUT:
  17.  * : The Morse code representation of the input string
  18.  *
  19.  *******************************************************************************/
  20.  
  21. #include <iostream>
  22. #include <string>
  23. #include <cctype> // For toupper function
  24.  
  25. using namespace std;
  26.  
  27. // Function prototypes
  28. void convertToMorse(const string &input, const string morseCodes[]);
  29. int getMorseIndex(char ch);
  30.  
  31. // Morse code array for characters and symbols
  32. const string MORSE_CODES[] = {
  33. " ", "--..--", ".-.-.-", "..--..", "-----", ".----", "..---", "...--",
  34. "....-", ".....", "-....", "--...", "---..", "----.", ".-", "-...",
  35. "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
  36. "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-",
  37. ".--", "-..-", "-.--", "--.."
  38. };
  39.  
  40. int main() {
  41. string inputString;
  42.  
  43. // Prompt the user for input
  44. cout << "Enter your string\n";
  45. cout << "(Max 100 characters):\n";
  46. getline(cin, inputString); // Read the entire line of input
  47.  
  48. // Convert the input string to Morse Code
  49. cout << "\nMorse Code Conversion:\n";
  50. convertToMorse(inputString, MORSE_CODES);
  51.  
  52. return 0;
  53. }
  54.  
  55. // Function to convert the input string to Morse Code
  56. void convertToMorse(const string &input, const string morseCodes[]) {
  57. for (char ch : input) {
  58. // Convert character to uppercase and find Morse index
  59. int index = getMorseIndex(toupper(ch));
  60.  
  61. // If the index is valid, print the Morse code
  62. if (index != -1) {
  63. cout << morseCodes[index] << " ";
  64. } else {
  65. cout << "[Invalid] "; // Handle unsupported characters
  66. }
  67. }
  68. cout << endl;
  69. }
  70.  
  71. // Function to get the Morse code array index for a character
  72. int getMorseIndex(char ch) {
  73. if (ch == ' ') return 0; // Space
  74. if (ch == ',') return 1; // Comma
  75. if (ch == '.') return 2; // Period
  76. if (ch == '?') return 3; // Question mark
  77. if (ch >= '0' && ch <= '9') return ch - '0' + 4; // Numbers 0-9
  78. if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 14; // Letters A-Z
  79. return -1; // Invalid character
  80. }
Success #stdin #stdout 0.01s 5280KB
stdin
Hello world
stdout
Enter your string, and I will convert it to Morse Code!
(Max 100 characters):

Morse Code Conversion:
.... . .-.. .-.. ---   .-- --- .-. .-.. -..