Function Name: caesarCipher Inputs: 1. (char) A string of unknown length 2. (dou
ID: 668321 • Letter: F
Question
Function Name: caesarCipher
Inputs:
1. (char) A string of unknown length
2. (double) An integer describing the shift
Outputs:
1. (char) A coded string using the Caesar cipher
Function Description:
Last week you wrote the function caesarSalad() to apply a Caesar shift to lowercase letters and encode a word. Now, we will extend that cipher to entire phrases! Using the base code from last week as a skeleton for one (or two) helper function(s), write a function in MATLAB named caesarCipher(). This function will take in a string with upper and lower case letters, as well as punctuation, and then apply the Caesar shift to the string, using the helper function(s) you have created to encode it. Only letters (both upper and lower case) should be encoded using the Caesar cipher. Any other characters, such as spaces, periods, etc., should not be altered.
Notes:
The cipher should work for both positive and negative integers that indicate the shift as given by the second input.
There is no limit to the value of the shift number in the second input.
You MUST use a helper function to solve this problem.
You may write your helper function(s) below your caesarCipher() function, or you may write it/them to separate files. If you write it/them to a separate file(s), you MUST include it/them in your overall file submission.
Hints:
The mod() function, and its effect on both positive and negative numbers, may be useful.
Consider editing caesarSalad() to have one helper function for capital letters and one for lowercase
Explanation / Answer
Algorithm :
Program :
function ceaser
clc
x=input(‘Enter Input Text =’,’s’);
len=length(x);
array=[];
input1=[];
key = input( ‘Enter Key Value’);
for i=1:len
input1(i)=x(i);
end
for i=1:len
array(i)=input1(i)+key;
%handling small alphabets
if array(i)>122 && input1(i)>=97
array(i)=array(i)-122;
array(i)=array(i)+96;
end
%handling capital alphabets
if array(i)>90 && input1(i)<=90
array(i)=array(i)-90;
array(i)=array(i)+64;
end
end
disp(‘Ecryption Result’);
ENCRYPT = char(array)
for i=1:len
array(i)=array(i)-key;
%handling small alphabets in Decryption
if array(i)<=97 && input1(i)>=97
array(i)=97-array(i);
array(i)=123-array(i);
end
%handling capital alphabets
if array(i)<65 && input1(i)<=90
array(i)=65-array(i);
array(i)=91-array(i);
end
end
disp(‘Decryption Result’);
DECRYPT = char(array)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.