Reputation: 37
I have entered a sentence of type string.
std::string message;
std::getline(std::cin, message);
After entering a sentence I used a if statement to convert the string to "Morse code":
int length = message.length();
for(int i = 0; i < length;i++) //to loop in the message
{
if(message[i] == 'A')
cout << "-.";//and the rest for 'b','c','d'....'z'
}
How do i take the Morse code of the string entered and decode it. Eg: If the is ".-" in the Morse code then display 'A' and if "-..." in the message display 'B'.
Upvotes: 1
Views: 219
Reputation: 3691
use a binary tree this way - the root is empty (NULL). each child well have one of the chars '-' '.' . this way you decode the hole Morse code into the tree. now instead of NULL put at the end put the char that you should get at the end. the tree should look like that:
root / \ '-' '.' \ '.' \ 'A'
etc. now you could find chars in O(lg n) when n = size of tree.
Upvotes: 8