Write a Matlab function called swapcodem that takes a string
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\'
Solution
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\'

