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

% Function Name: madlibStory % Inputs (2): - (char) name of text file % - (char)

ID: 3538038 • Letter: #

Question

% Function Name: madlibStory
% Inputs (2): - (char) name of text file
%              - (char) name of xls file
% Function Description:
%   Write a function called "madLibStory" where the first input is the name of a
%   text file containing a story with the keys: %n, %v, or %a. And the
%   second input is the name of an excel spreadsheet containing the words
%   to insert in 3 columns: 'Nouns', 'Verbs' and 'Adjectives'.
%
%   Replace the keys in the text file with their corresponding part of
%   speech from the excel file, and write the result to a new file named
%   the same as the original text file with the suffix '_done.txt'.
%   Use the words from the excel spreadsheet in the order they are listed.
%   And, if you run out, loop back to the beginning of the column to
%   replace remaining keys of that type.
% Note:
%   - The columns may appear in different orders, but will always have the
%   same 3 header names: 'Nouns', 'Adjectives', and 'Verbs'.
% Test Cases:
% file1a = 'valentineMadlib.txt';

valentineMadlib reads

Dear Sweetheart,

Sometimes I think back to how we met. We saw each other and instantly knew we'd be %n.
But I wanted more.
You seemed so %a; I just wanted to know everything about you. You were so %a and such a gentleman. It was a lovely surprise.
So hard to find a gentleman in this %n.
But that's not all. You can %v better than anyone I have ever met. And you always hold my hand when we %v in the %n together.
Someday, I will kiss you on the cheek as we watch the sunset and munch on %n.

Be mine forever.

Love,
Your little %n

% file1b = 'madlibWords1.xls';
% madlibStory(file1a,file1b)---> creates file valentineMadlib_done.txt
% (content should match that of the provided valentineMadlib_doneSOLN.txt file)


% file2a = 'DennisMadlib.txt';

DennisMadlib reads

There was once a boy named Dennis.
He had a big %n, but no %n.
His mother was %a, and his father was %a.
His baby brother was %a and cried too much.
Dennis wanted a dog to %v with.
But his %a parents said no.
Poor Dennis.

% file2b = 'madlibWords2.xls';
% madlibStory(file2a,file2b)---> creates file DennisMadlib_done.txt
% (content should match that of the provided DennisMadlib_doneSOLN.txt file)

Explanation / Answer

function madlibStory(inp1,inp2)

oup=strtok(inp1,'.');

oup=strcat(oup,'_done.txt');

text=fileread(inp1);

[num,t,all]=xlsread(inp2);

[r,c]=size(all);

n=0;

a=0;

v=0;


% get proper columns

for j=1:c

if strcmp(all{1,j},'Nouns')

n=j;

  

elseif strcmp(all{1,j},'Verbs')

v=j;

  

elseif strcmp(all{1,j},'Adjectives')

a=j;

end

end


%replace nouns

i=2;

while strfind(text,'%n')

text=regexprep(text,'%n',all{i,n},'once');

i=i+1;

if i>r

i=2;

elseif isnan(all{i,n})

i=2;

end

end


%replace adjectives

i=2;

while strfind(text,'%a')

text=regexprep(text,'%a',all{i,a},'once');

i=i+1;

if i>r

i=2;

elseif isnan(all{i,n})

i=2;

end

end


%replace verbs

i=2;

while strfind(text,'%v')

text=regexprep(text,'%v',all{i,v},'once');

i=i+1;

if i>r

i=2;

elseif isnan(all{i,n})

i=2;

end

end


%write file

ofile=fopen(oup,'w');

fprintf(ofile,'%s',text);

fclose(ofile);

end