fork download
  1. //Sam Partovi CS1A Ch. 10, P.591, #17
  2. /*******************************************************************************
  3. * CONVERT STRING TO MORSE CODE
  4. * ____________________________________________________________
  5. * This program accepts a string from the user, converts it into Morse code,
  6. * and displays the result.
  7. * ____________________________________________________________
  8. *INPUT
  9. * userString: The string entered by the user.
  10. *
  11. *OUTPUT
  12. * MorseCode: The converted string in Morse code.
  13. *******************************************************************************/
  14. #include <iostream>
  15. #include <string>
  16. using namespace std;
  17.  
  18. //Function prototypes
  19. void GetMorseCode(char c, string& morseCode);
  20. void ConvertToMorseCode(const string& input);
  21.  
  22. int main() {
  23. string userString;
  24.  
  25. // Prompt the user to enter a string
  26. cout << "Enter a string to convert to Morse code: ";
  27. getline(cin, userString);
  28.  
  29. // Convert and display the Morse code
  30. ConvertToMorseCode(userString);
  31.  
  32. return 0;
  33. }
  34.  
  35. /*******************************************************************************
  36. *Function definition for GetMorseCode
  37. * This function finds the corresponding morse code for individual characters.
  38. *******************************************************************************/
  39. void GetMorseCode(char c, string& morseCode) {
  40. //Arrays for characters and their corresponding Morse codes
  41. const char characters[] = {
  42. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
  43. 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  44. 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '
  45. };
  46. const string morseCodes[] = {
  47. ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
  48. "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
  49. "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--",
  50. "....-", ".....", "-....", "--...", "---..", "----.", "-----", " "
  51. };
  52. const int numCharacters = sizeof(characters) / sizeof(characters[0]);
  53.  
  54. char upperChar = toupper(c); //Convert to uppercase for consistency
  55.  
  56. //Search for the character in the characters array
  57. for (int i = 0; i < numCharacters; i++) {
  58. if (characters[i] == upperChar) {
  59. morseCode = morseCodes[i]; // Get the Morse code
  60. break;
  61. }
  62. }
  63. }
  64.  
  65. /*******************************************************************************
  66. *Function definition for ConvertToMorseCode
  67. * This function converts given input to its morse code equivalent.
  68. *******************************************************************************/
  69. void ConvertToMorseCode(const string& input) {
  70. cout << "\nMorse Code: ";
  71. for (char c : input) {
  72. string morseCode;
  73. GetMorseCode(c, morseCode); //Get Morse code for the character
  74. cout << morseCode << " "; //Output the Morse code
  75. }
  76. cout << endl;
  77. }
  78.  
Success #stdin #stdout 0s 5276KB
stdin
one two three 1 2 3
stdout
Enter a string to convert to Morse code: 
Morse Code: --- -. .   - .-- ---   - .... .-. . .   .----   ..---   ...--