Function Name: encodeCipher Inputs (1): - (char) A string of characters Outputs
ID: 3537407 • Letter: F
Question
Function Name: encodeCipher Inputs (1): - (char) A string of characters Outputs (1): - (char) An encoded string of characters Function Description:The function takes in a string and returns the string rearranged such that the first letter is put with the last letter, then the second letter and the second to last letter, For example, 'phones' would become 'psheon'. This is because:p is the first index s is the last index h is the (first + 1) index e is the (last - 1) indexetc. etc. When combined, you get 'psheon' Notes: - If the string has an odd number of letters, the middle letter should become the last letter in the string. - Treat punctuation/spaces the same as letters. Test Cases: a = encodeCipher('rockstar'); a => 'rroactks' b = encodeCipher('Explanation / Answer
function y =encodeCipher(s)
l=length(s);
res=strcat(s(1),s(l));
if mod(l,2) == 0
k='';
else
k=s((l/2)+0.5);
end
for i=1:((l/2)-0.5)
res=strcat(res,s(1+i),s(l-i));
end
res=strcat(res,k);
y=res;
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.