Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // Declare the "morse letter map"
- //
- // Both these "arrays" contain the same number of items
- // (A string is essentially an array of characters).
- // Each item in the morse array represent the item (letter)
- // in the same position in the alphabet array.
- //
- // The reason we use this method is because we have a
- // big variation of letters to translate (åäö not being
- // right next to the rest of the letters. If they were
- // we could have used a more optimal solution)
- string alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZåäö";
- string morse[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".--.-",".-.-","---.",};
- // Translates a normal character to it's morse string representation.
- // The function assumes the letter is already in uppercase to match our alphabet.
- string morseFromLetter(char letter)
- {
- // Make sure we have the uppercase version of the letter,
- // since our alphabet is in uppercase (this practically means that
- // the program will not make any difference between cases)
- char uppercaseLetter = toupper(letter);
- // Go through the alphabet and find index of the letter we want to translate.
- // We will use this index to select the correct morse representation from the morse array.
- int alphabetLength = alphabet.length();
- for(int i = 0; i < alphabetLength; i++)
- {
- // Compare the current alphabet letter to the input letter,
- // if they match we have found the correct index.
- if(alphabet[i] == uppercaseLetter)
- {
- // Select the translation from the morse
- // array and have the function return it
- return morse[i];
- }
- }
- // Reaching this point means that the letter wasn't found in the alphabet we have defined.
- // In this case we will return an empty string, since we don't know what else to return.
- return "";
- }
- // Translates a normal string of text
- // to it's morse string representation
- string morseFromText(string text, string padding = " ")
- {
- // Retreive the length of the text
- int textLength = text.length();
- // This variable will get each letters translation added to it,
- // and of course we then want to start with an empty string
- string textTranslation = "";
- // Go through each letter of the text (one letter per loop)
- for(int i = 0; i < textLength; i++)
- {
- // Retreive the letter of the current loop
- char currentLetter = text[i];
- // Send currentLetter to our letter translation
- // function to get the morse representation of the letter
- string letterTranslation = morseFromLetter(currentLetter);
- // Add the translation of the current letter
- // to the full text translation variable
- textTranslation += letterTranslation;
- // Add the padding text that we want
- // to have between every morse string
- textTranslation += padding;
- }
- // Have the function return the result,
- // the text translated into morse code
- return textTranslation;
- }
- int main()
- {
- // These are the variables we will need for the loop,
- // also it looks a lot neater to have them here.
- string input, translation;
- // Start by welcoming the user and giving
- // instructions on how to use the program
- cout << "Welcome to the morse code translator!" << endl << endl
- << "After '>', type any text you want to translate " << endl
- << "and press Enter to receive the translation. " << endl << endl
- << "To end the program simply submit 'quit'." << endl << endl
- << "Have fun!" << endl;
- // Loop until "further notice" :)
- while(true)
- {
- // Have the user input something to translate
- cout << "> ";
- cin >> input;
- // Check if the user wants to quit the program.
- if(input == "quit")
- break; // Stop looping if a quit request is detected
- // Translate what the user input into morse code
- translation = morseFromText(input);
- // Output the translation to the user
- cout << "= " << translation << endl << endl;
- // Loop again!
- }
- // Require the user to press any key
- // before finally ending the program
- system("pause");
- }
Add Comment
Please, Sign In to add comment