Function Name: coldwar Inputs: 1. 2. 3. (char) a scrambled character vector (dou
ID: 2267393 • Letter: F
Question
Function Name: coldwar Inputs: 1. 2. 3. (char) a scrambled character vector (double) an integer representing the shift of odd indices (double) an integer representing the shift of even indices Outputs: 1. (char) the unscrambled character vector Background: While enjoying the winter weather with some pals, you decide to have a snowball fight. Your teammate, on the other side of Tech Green, wants to communicate a frosty message to you without the opposing side being able to decode it. They decide to shout a scrambled version of your message along with two numbers that represent shift values. Since your and your friend are well versed in MATLAB, you decide to write a quick function to decode the encrypted message. Function Description: You must 'shift the letters at even indices by a certain amount and the letters at odd indices by a different amount. For example, if the letter is 'a' and the shift amount is 4 the letter will then become 'e'. A negative shift means shifting towards 'a' in the alphabet. The shift should 'wrap' around the alphabet- 'x' shifted by 7 should become 'e' Example: [out] = coldar("usph',2,-4) out = 'word' Notes: There will be no spaces or uppercase letters. The shift may be greater than 26. A positive shift of 28 is the same as a shift of 2. Hints: mod() will be useful.Explanation / Answer
function word = coldWar(message,odd,even)
word=double(message);
o=mod(odd,26);
e=mod(even,26);
for i=1:2:length(word)
word(i)=word(i)+o;
if word(i)>122
word(i)=word(i)-122;
word(i)=word(i)+96;
end
if word(i)<97
word(i)=97-word(i);
word(i)=123-word(i);
end
end
for i=2:2:length(word)
word(i)=word(i)+e;
if word(i)>122
word(i)=word(i)-122;
word(i)=word(i)+96;
end
if word(i)<97
word(i)=97-word(i);
word(i)=123-word(i);
end
end
word=char(word);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.