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

Function Name: acronym % Inputs (1): - (string) a 1xM string containing a phrase

ID: 3541806 • Letter: F

Question

Function Name: acronym
% Inputs  (1): - (string) a 1xM string containing a phrase
% Outputs (1): - (string) A 1xN string containing an acronym of the given
%                         phrase
%
% Function Description:
%   Write a function called "acronym" that takes in a phrase and outputs
%   an acronym of the phrase.  The output should contain only the first
%   letter of every word and only if the first letter is capitalized.
%   There will always be a phrase, and the phrase can contain any number of
%   words.  The phrase will always produce a valid acronym.
%
%   Example:
%       input: 'Boy Scouts of America'
%       output: 'BSA'
%
%   Note:
%       You must use logical indexing to solve this problem.  
%
%
% Test Cases:
%   out1 = acronym('Game of Thrones');
%        => 'GT'
%
%   out2 = acronym('You Obviously Like Owls');
%        => 'YOLO'
%
%   out3 = acronym('Time And Relative Dimension In Space');
%        => 'TARDIS'

Explanation / Answer

function out = acronym(in)

%split into words

in = regexp(in,'s+','split');

s = max(size(in));

inp=[];

% get first letter of each word

for k=1:s

inp = [inp in{k}(1)];

end

% using logical indexing

out = inp( isstrprop(inp,'upper'));

end