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

% Function Name: encodeCipher % Inputs (1): - (char) A string of characters % Ou

ID: 3537399 • Letter: #

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, etc. % % 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) index % etc. 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 output_str = encodeCipher(input_str)

l = length(input_str);
if (l%2 == 0)        % even case
    for i=1:l/2          
       output_str(2*i-1) = input_str(i);
       output_str(2*i) = input_str(l-i+1);
    end;

else                  % odd case               
    output_str(l)=input_str((l+1)/2);
    for i=1:(l-1)/2
       output_str(2*i-1) = input_str(i);
       output_str(2*i) = input_str(l-i+1);
    end;
end;
  
end;