i need help please!!!!! decoding morse code

8 views (last 30 days)
anthony
anthony on 12 Dec 2013
Commented: Nishant Kumar on 8 Nov 2014
can someone please help me write a program to convert text messages into Morse code. For simplicity, only letters and digits are allowed in text messages. Because punctuation marks or special symbols are not allowed to appear in text messages, words are only separated by spaces.
At the beginning part the program, you may copy and reuse the following code snippet to define the mapping between letters/digits and their Morse code (case insensitive), for example, MC_1 is the Morse code of digit 1, and MC_A is the Morse code of letter 'A' or letter 'a'.
MC_1='.----'; MC_2='..---'; MC_3='...--';
MC_4='....-'; MC_5='.....'; MC_6='-....';
MC_7='--...'; MC_8='---..'; MC_9='----.';
MC_0='-----'; MC_A='.-'; MC_B='-...';
MC_C='-.-.'; MC_D='-..'; MC_E='.';
MC_F='..-.'; MC_G='--.'; MC_H='....';
MC_I='..'; MC_J='.---'; MC_K='-.-';
MC_L='.-..'; MC_M='--'; MC_N='-.';
MC_O='---'; MC_P='.--.'; MC_Q='--.-';
MC_R='.-.'; MC_S='...'; MC_T='-';
MC_U='..-'; MC_V='...-'; MC_W='.--';
MC_X='-..-'; MC_Y='-.--'; MC_Z='--..';
Then your program uses input function to accept a string as a text message. After that, the program examines the characters from the beginning to the end and translates them into Morse code. The program may use a string (e.g. Morse_Code) to accumulate the Morse code of the characters: for each character being examined, the program saves its Morse code into the string, e.g. if the character being examined is digit 3, the program uses statement Morse_Code = [Morse_Code ' ' MC_3] to save the Morse code of digit 3 into the string, or if the character being examined is letter A, the program uses statement Morse_Code = [Morse_Code ' ' MC_A]. When the program finishes examining all the characters, the string will have the Morse code of the text message in it.
During the translation, in addition to the letters/digits, your program needs to handle the spaces in the text message, which separate words. If a punctuation mark or special symbol is met, your program should stop the translation and print out an error message, indicating that the text message is illegal. When the translation finishes, your program prints out the text.
Please pay attention that spaces and slashes ('/') are only separators in the resulting Morse code. They cannot appear at the end of the Morse code. the Morse code you get must be ended with either dot ('.') or dash ('-').
Consecutive space characters in the text message should be consolidated and viewed as a single space. Thus both 'MATLAB Programming' and 'MATLAB Programming' (the one with more spaces between the two words) have the same Morse code.
  4 Comments
anthony
anthony on 12 Dec 2013
Edited: Walter Roberson on 12 Dec 2013
i am not asking you to do my homework i just need a little help i got as far a as this
for x=1:2
str=input ('please enter text message you want to morse code: \n','s');
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letter={'1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_'};
for i=1:length(letter)
if strcmpi(str,letter(i))==1
disp(morse(i));
Y(1,x)= morse(i);
end
end
end
disp(Y);
BUT I CANT GET IT TO DO MULTIPLE CHARACTER LIKE IF I WAS TYPE IN "HELLO MY NAME IS" I AM JUST CONFUSED
Nishant Kumar
Nishant Kumar on 8 Nov 2014
str=input ('please enter text message you want to morse code: \n','s');
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letter={'1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_'};
for i=1:length(str)
for j=1:length(letter)
if strcmpi(str(i),letter(j))==1
disp(morse(j));
end
end
end

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 12 Dec 2013
Not a bad start. But you didn't convert str to upper case, which you might want to do, and you should use ismember instead of strcmpi, and the for loop doesn't go to the length of letter but should scan str instead, like this:
str = input ('Please enter text message you want to morse code: \n','s');
str = upper(str);
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letter={'1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_'};
for i=1:length(str)
[~, index] = ismember(str(i), letter);
if index > 0
fprintf('%s ', morse{index});
end
end
fprintf('\n');
  3 Comments
anthony
anthony on 12 Dec 2013
thank you thank you thank you all
Image Analyst
Image Analyst on 12 Dec 2013
If that's enough to keep you going, then could you go ahead and mark it accepted?

Sign in to comment.


Walter Roberson
Walter Roberson on 12 Dec 2013
Your line
strcmpi(str,letter(i))
is comparing all of "str" to the single letter letter(i) . So if str is more than one character, then the comparison is not going to work.
If you are going to use that approach to find the matching characters, then you need to have another level of looping in which you loop over each character in str.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!