Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Matlab function called swapcode.m that takes a string msg as input and r

ID: 3931876 • 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 with 'y', 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

swapcode.m file

function y = swapcode(x)

charsL = char(97:122);

charsU = char(65:90);

charsl = fliplr(charsL);

charsu = fliplr(charsU);

y = '';

for i=1:length(x)

c = x(i);

if(isletter(c))

if(ismember(c,charsL))

for j=1:26

if(eq(c,charsL(j)))

cc = charsl(j);

y = strcat(y,cc);

break;

end

end

else

if(ismember(c,charsU))

for j=1:26

if(eq(c,charsU(j)))

cc = charsu(j);

y = strcat(y,cc);

break;

end

end

end

end

else

y = strcat(y,{c});

end

end

end

test.m file

x = input('Enter string: ','s');

rev = swapcode(x);

disp(rev);

Output:

Enter string: This is a sentence

'Gsrh rh z hvmgvmxv'

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote