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

A shift cipher is a very old method of encoding messages. In order to encode a m

ID: 3827180 • Letter: A

Question

A shift cipher is a very old method of encoding messages. In order to encode a message, the user must pick a shift value and then shift every letter of the alphabet in the message to the right by that value (for example, with a shift value of 3, a becomes d, b becomes e, etc.). To decode the message, the shift value must be communicated with the encoded message, and then every letter of the alphabet in the encoded message must be shifted to the left by the shift value to recover the original message. Write a Matlab function called shifter.m which will employ the shift cipher method to encode a message. Your function should take 2 input values: msg (the message to be encoded) and shiftval (the number of letters to shift the alphabet). The function should return the encoded message as a string called shifted. Additionally, your function should satisfy the following properties: if shiftval is not an integer, then shifted should be set to false regardless of the value of msg for any value of msg, the function call shifter (msg, 0) should simply return the original value of msg for any value of msg and any integer value of shiftval, if coded = shifter (msg, shiftval), then msg = shifter (coded, -shiftval) Punctuation, spaces, and numbers should be ignored and letters which are upper/lower case should remain upper/lower case after the shift.

Explanation / Answer

Matlab code:

function shifted =shifter(msg,shiftval)

%find the message length
msglen=length(msg);
alphab=char( [ (65:90)'; (97:122)'] );

%to set the shifted vector
for i=1:numel(alphab)
num=double(alphab(i));
if ( num>=65 && num<=90 )
num=mod(num-65+shiftval,26);
Shitedalpha(i,1)=char(num+65);
elseif ( num>=97 && num<=122 )
num=mod(num-97+shiftval,26);
Shitedalpha(i,1)=char(num+97);
end
end

%for loop to find the shifted message
for vi=1:msglen
for vj=1:52
if alphab(vj)==msg(vi)
pos=vj;
end
end
shifted(vi)=Shitedalpha(pos);
end
end

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