+1 (315) 557-6473 

Python Program to Implement Strings and Arrays Assignment Solution.


Instructions

Objective
Write a python assignment program to implement strings and arrays.

Requirements and Specifications

Write a function that takes a cell array of strings an prints them, one per line, in a rectangular frame. For example the list {'Hello', 'World', 'in', 'a', 'frame'} gets printed as: ********* * Hello * * world * * in * * a * * frame * ********* Note that the width of the frame will depend on the longest word. Any words shorter than the longest one will need some additional spaces to fill in the frame.
Source Code
function printArray(cell_arr)
% Calculate length of longest word
N = 0;
for i = 1:length(cell_arr)
if strlength(cell_arr{i}) > N
N = strlength(cell_arr{i});
end
end
% The width of the frame is 4 characters more than the longest word
width = N+4;
% Now print
fprintf(repelem('*', width+2));
fprintf("\n");
% Now print each word
for i = 1:length(cell_arr)
word = cell_arr{i};
% Calculate number of spaces to append at left-right
n_spaces = floor((width-2-strlength(word))/2); % calculate total number of of required spaces
sp_left = n_spaces; % total spaces to be put at the left of the word
sp_right = width-2-strlength(word)-sp_left; % total spaces to be put at the right
fprintf(join(strcat(["*", join(repelem("", sp_left)), word, join(repelem("", sp_right)), "*"]))); % print
fprintf("\n");
end
fprintf(repelem('*', width+2));
fprintf("\n");
end