MATLAB: Given the three strings \'GR PXE\',\'NTEMEH\' and \'ISLA T\', the string
ID: 3872127 • Letter: M
Question
MATLAB: Given the three strings 'GR PXE','NTEMEH' and 'ISLA T', the string created by braiding the strings together will be 'GNIRTS ELPMAXE EHT'. Continuing with this example, your final output string would read: 'THE EXAMPLE STRING'
Input is three seperate strings, output is one large decoded string. Below is the code I have so far in Matlab, I'm stuck with where to go from here:
function [ newString ] = braid( string3, string2, string1 )
%braid: Decode seperate strings into a message
% Detailed explanation goes here
N = 3.*(length(string1));
vec1 = 1:3:N;
vec2 = 2:3:N;
vec3 = 3:3:N;
string1 = fliplr(string1);
string2 = fliplr(string2);
string3 = fliplr(string3);
end
Also, for reference, below is an example of an input and the proper output:
Test Cases:
[decoded1] = braid(' eo teou', ' lre vso', 'mbphdl y')
decoded1 => you solved the problem
Explanation / Answer
Given below is the completed function braid(). Please save the below code in a file named braid.m and then test it at command prompt. Hope the answer helps. If it did, please don't forget to rate the answer . Thank you very much.
function [ newString ] = braid( string3, string2, string1)
% braid: Decode seperate strings into a message
% Detailed explanation goes here
n1 = length(string1); %length of each of the input strings
n2 = 3 * n1; % the length of new string
newString(1:n2) = ' '; %initialize the newString to contain all blanks
idx1 = 1; %index into each of input strings
idx2 = 1; %index into newString
while idx1 <= n1
newString(idx2) = string3(idx1);
newString(idx2 + 1) = string2(idx1);
newString(idx2 + 2) = string1(idx1);
idx1 = idx1 + 1; %increment to next character from input strings
idx2 = idx2 + 3; %increment to next position to fill in newString
end
newString = fliplr(newString); % reverse the string
end
=====================
output
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.