% Function Name: madLib % Inputs (2): - (string) a sentence containing a key to
ID: 3537015 • Letter: #
Question
% Function Name: madLib
% Inputs (2): - (string) a sentence containing a key to be replaced with a
% n, v, or adj
% - (string) a comma-delimited list of n, v, adj replacements
% Outputs (1): - (double) The original sentence with the key replaced by
% the correct part of speech
%
% Function Description:
% Write a function called "madLib" that takes a sentence with the key: %n,
% %v, or %a, and replaces that key with the noun, verb, or adjective
% provided in the second input. Each sentence will contain only one key,
% and the second input will always be a comma-delimited list of a noun,
% a verb, and an adjective, in exactly that order.
%
% Test Cases:
% sentence1 = 'Don''t %v believin''.';
% sentence2 = 'I''m a Ramblin Wreck from %n and a helluva engineer.'
% sentence3 = 'Hit me with your %a shot.'
%
% fullSent1 = madLib(sentence1, 'butterfly,stop,happy');
% fullSent1 => 'Don''t stop believin''.'
%
% fullSent2 = madLib(sentence2, 'Georgia Tech,smile,blue');
% fullSent2 => 'I''m a Ramblin Wreck from Georgia Tech and a Helluva engineer.'
%
% fullSent3 = madLib(sentence3, 'burrito,climb,best');
% fullSent3 => 'Hit me with your best shot.'
Explanation / Answer
function newSentence = madLib(sentence,replacements)
reps=regexp(replacements,',','split');
sentence =regexprep(sentence,'%n',reps{1});
sentence =regexprep(sentence,'%v',reps{2});
newSentence =regexprep(sentence,'%a',reps{3});
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.