Write a Matlab function called svapcode m that takes a strin
Write a Matlab function called svapcode. 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 ^3a^3 with ^3z^3. each ^3b^3 with ^3y^3, each ^3c^3 with ^3x^3, 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 svapcode (svapcode(txtin)) then the string stored in txt out 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 svapcode( \'This is a sentence.) produces the string \'Gsrh rh z hvmgvmxv.^3 suapcodeC^3 Who has a 3-legged dog?\') produces the string \'Dsl szh z 3-ovttvw wit?\' svapcode (swapcode(^3 Dave97^3)) product\'s the string \'Dave97\'
Solution
function coded = swapcode(msg)
coded = msg;
arr = [\'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(msg)
temp = isletter(msg)(i);
if temp == 1
coded(i) = fliplr(arr)(int8(double(lower(msg(i))) - double(\'a\') + 1));
if msg(i) - \'a\' < 0
coded(i) = upper(coded(i));
end
end
end
end
