Function Name: drSeuss Inputs: 1. (char) The name of a text file, including file
ID: 3588569 • Letter: F
Question
Function Name: drSeuss
Inputs:
1. (char) The name of a text file, including file extension
Outputs:
1. (char ) A sentence describing the rhyme scheme
Background:
Imagine you hopped in a time machine and went back to 1950 to help Dr. Seuss write a
few of his books. However, before you can help write any books, Dr. Seuss wants you to prove
yourself by analyzing the rhyme scheme of his different books. Armed with technology from the
future, write a MATLAB function to analyze the rhyme schemes of Dr. Seuss's famous books.
Function Description:
Given a text file, write a function that first checks if the first and second line in the text file
rhyme. If they do, output the string:
'Wow! Line one and two rhyme, reading this will be a great time.'
If line one and two don't rhyme with each other, check if the first and third line in the text file
rhyme. If they do, output the string:
'Wow! Line one and three rhyme, reading this will be a great time.'
If neither the first and second line nor the first and third line rhyme, output the string:
'In line one, two, or three, rhymes were not meant to be.'
Example:
Text File:
Too wet to go out and too cold to play ball
So we sat in the house
We did nothing at all
Output:
'Wow! Line one and three rhyme, reading this will be a great
time.'
Notes:
To check if lines rhyme, compare the last two letters of the last word on each line.
Remember that uppercase and lowercase don't contribute to whether or not words
rhyme, so this comparison should be case-insensitive.
Each text file will contain 3 lines of text.
None of the lines will end with a punctuation mark.
Explanation / Answer
fid = fopen('rythm.txt'); %open .text file
line_no = 0; %count line read
i=1;
while ( line_no ~= 3) %count upto three lines
line_in = fgets(fid); % load first line in .txt file in line_in
%fgets include newline char therefore line 1 and 2 last letter are at
%end-2 and end-3 whereas third line have no newline char
if line_no == 2
a(i) = line_in(end-1); % second last letter of line third is store in a
a(i+1)= line_in(end); %last letter of line third is store in a
else
a(i) = line_in(end-3); % second last letter of line first and second is store in a
a(i+1)=line_in(end-2); %last letter of line first and second is store in a
end
i=i+2; %increment i to store last and second last letters of other lines
line_no =line_no+1; %increment line read
end
% compare last two letter of line 1 and line 2
if(a(1)== a(3) && a(2)== a(4))
disp('Wow! Line one and two rhyme, reading this will be a great')
% compare last two letter of line 1 and line 3
else if(a(1)== a(5) && a(2)== a(6))
disp('Wow! Line one and three rhyme, reading this will be a great')
else
disp('In line one, two, or three, rhymes were not meant to be.')
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.