i need help please!!!!! decoding morse code
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
Nishant Kumar
2014-11-8
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
回答(2 个)
Image Analyst
2013-12-12
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 个评论
Image Analyst
2013-12-12
If that's enough to keep you going, then could you go ahead and mark it accepted?
Walter Roberson
2013-12-12
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!