% Function Name: caesar % Inputs (2): - (char) a string of unknown length % - (d
ID: 3537021 • Letter: #
Question
% Function Name: caesar
% Inputs (2): - (char) a string of unknown length
% - (double) an integer describing the shift
% Outputs (1): - (char) a coded string using the Caesar cipher
%
% Caeser Cipher Information:
% The Caesar cipher is named after Julius Caesar, who, according to
% Suetonius, used it with a shift of three to protect messages of
% military significance. It is unknown how effective the Caesar cipher
% was at the time, but it is likely to have been reasonably secure
% because most of Caesar's enemies would have been illiterate and
% others would have assumed that the messages were written in an unknown
% foreign language.
%
% Caesar ciphers can still be found today in children's toys such as
% secret decoder rings. A Caesar shift of thirteen is also performed in
% the ROT13 algorithm, a simple method often used to obscure text such as
% joke punchlines and spoilers online.
%
% In the Caesar cipher, each letter is shifted by the specified amount.
% For example, if the shift is 3, then the letter 'a' would be coded as
% the letter 'd'.
%
% Function Description:
% The function takes in a string and then uses the Caesar cipher to
% encode it. Only letters (both upper case and lower case) should be
% encoded using the Caesar cipher. Any other characters such as spaces,
% periods, etc., should not be encoded.
%
% Notes:
% - The Caesar cipher should work for both positive and negative integers
% that indicate the shift given by the second input.
% - There is no limit to the value of the shift number in the second
% input.
%
% Hints:
% - The mod() function may be useful.
%
% Test Cases:
% coded1 = caesar('Thank you Mario!', 4);
% coded1 => 'Xlero csy Qevms!'
%
% coded2 = caesar('Iba vby Wypujlzz pz pu huvaoly jhzasl!', -7);
% coded2 => 'But our Princess is in another castle!'
%
% coded3 = caesar('Uh, why isn''t this cipher working?', -26);
% coded3 => 'Uh, why isn''t this cipher working?'
%
% coded4 = caesar('uggcf://jjj.lbhghor.pbz/jngpu?i=9LsJE438jxt', 65);
% coded4 => 'https://www.youtube.com/watch?v=9YfWR438wkg'
PLEASE BRIEFLY EXPLAIN SYNTAX
Explanation / Answer
function ascii_out= caesar(encrypted, shift) ascii_out = double(encrypted); for i=1:length(ascii_out) if ascii_out(i) >=65 && ascii_out(i) 90 ascii_out(i) = 64+ ascii_out(i) - 90; end end if ascii_out(i) >=97 && ascii_out(i) 122 ascii_out(i) = 96 + ascii_out(i) - 122; end end end ascii_out = char(ascii_out); % converting back from ascii to letters end; coded1 = caesar('Thank you Mario!', 4); coded1 coded2 = caesar('Iba vby Wypujlzz pz pu huvaoly jhzasl!', -7); coded2 coded3 = caesar('Uh, why isn''t this cipher working?', -26); coded3 coded4 = caesar('uggcf://jjj.lbhghor.pbz/jngpu?i=9LsJE438jxt', 65); coded4Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.