Write a Matlab function called swapcode. m that takes a string msg as input and
ID: 3931942 • Letter: W
Question
Write a Matlab function called swapcode. m that takes a string msg as input and returns another string coded as output. The function encodes the string by reversing the alphabet: it replaces each 'a' with 'z', each 'b' with each 'c' with 'x' etc. The function must work for uppercase letters the same way, but it must not change any other characters. Note that if you call the function twice like this txtout = swapcode(swapcode(txtin)) then the string stored in txtout will be identical to the string stored in txtin. The specifications for the function and some sample function calls are shown below. input parameter msg a string output parameter coded a string sample function calls swapcode('This is a sentence.') produces the string 'Gsrh rh z hvmgvmxv.' Swapcode('Who has a 3-legged dog?') produces the string 'Dsl szh z 3-ovttvw wlt?' swapcode(swapcode('Dave97')) produces the string "Dave97'Explanation / Answer
msg=input('Enter the text:','s');
A='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
B='abcdefghijklmnopqrstuvwxyz';
tf=isletter(msg);
for i=1:length(msg)
if tf(i:i)==1
c=msg(i:i);
t=double(c);
if t<=90
t=t-65;
t=double(A(26-t:26-t));
end
if t>96
t=t-97;
t=double(B(26-t:26-t));
end
msg(i:i)=char(t);
end
end
fprintf('Coded string is:%s ',msg);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.