Write a Matlab function called swapcode m that takes a strin

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\'

Solution

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);

 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 reve

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site