Please help me writting a program that converts words to its Morse code using Ma
ID: 3771658 • Letter: P
Question
Please help me writting a program that converts words to its Morse code using Matlab. (You may use either a for loop or a while loop). For example, for a word 'AB', its Morse code is '.- -...' , since the Morse code of 'A' is '.-' and the Morse code of 'B' is '-...' . It is not correct to put '.-' and '-...' together without the space in between (i.e. '.--...'). Make sure that using MatLab cell arrays (e.g. {'.--.'; '.-.'; '---'; ...}) is not allowed. Somebody helped me before with this answer below, however I am not allow to use cell array like this (e.g. {'.--.'; '.-.'; '---'; ...}) : Please change the way to solve it.
inputstr = input ('Please enter the string to convert to morse code: ','s');
inputstr = upper(inputstr); %% converting the string to upper case
morse_dict={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'}; %% defining morse code for the characters
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','_'}; %%each character mapped to specific morse code
for i=1:length(inputstr)
[~, index] = ismember(inputstr(i), letter);
if index > 0
fprintf('%s ', morse_dict{index});
end
end
fprintf(' ');
Explanation / Answer
str = input ('Please enter text message you want to morse code: ','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(' ');
thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.