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

Mary had a little lamb, it\'s fleece was white as snow. And everywhere that Mary

ID: 3876516 • Letter: M

Question

Mary had a little lamb, it's fleece was white as snow. And everywhere that Mary went, the lamb was sure to go. Write a program which reads a text file, splits the lines into the individual words and stores the word lengths in an array. You may assume that words are only separated by space characters or line separators. Ensure you test your program in the case where two words are separated by more than one space character. Report the total number of words, the number of characters within the words (excluding spaces but including punctuation marks) and the lengths of the individual words. The file has 22 word(s) containing 89 character(s). Word 1 has 4 character(s) Word 2 has 3 character(s) Word 3 has 1 character(s) Word 4 has 6 character(s) Word 5 has 5 character(s) Word 6 has 4 character(s) Word 7 has 6 character(s) Word 8 has 3 character(s) Word 9 has 5 character(s) Word 10 has 2 character(s) Word 11 has 5 character(s) Word 12 has 3 character(s) Word 13 has 10 character(s) Word 14 has 4 character(s) Word 15 has 4 character(s) Word 16 has 5 character(s) Word 17 has 3 character(s) Word 18 has 4 character(s) Word 19 has 3 character(s) Word 20 has 4 character(a) Word 21 has 2 character(s) Word 22 has 3 character(s)

Explanation / Answer

% File input.txt contains the text to be read.

% The program reads the file into a string and splits it into an array of words

% Then it counts the total number of characters in all the words in teh array and prints it out.

% Finally it reports the characters in each word in the file.

str=fileread('input.txt')
words = strsplit(str)
nWords = numel(words);
sum=0
for k=1:nWords
sum=sum+numel(words{k});
end

fprintf('The file has %d words containing %d character(s). ',nWords,sum);
for k=1:nWords
fprintf('* Word %d has %d character(s). ',k,numel(words{k}));
end

----------------------------------------------------------------------------------------------------------------------------------

Sample output for the input given in the question:

The file has 22 words containing 89 character(s).

* Word 1 has 4 character(s).   

* Word 2 has 3 character(s).   

* Word 3 has 1 character(s).   

* Word 4 has 6 character(s).   

* Word 5 has 5 character(s).   

* Word 6 has 4 character(s).   

* Word 7 has 6 character(s).

* Word 8 has 3 character(s).   

* Word 9 has 5 character(s).   

* Word 10 has 2 character(s).   

* Word 11 has 5 character(s).

* Word 12 has 3 character(s).   

* Word 13 has 10 character(s).   

* Word 14 has 4 character(s).   

* Word 15 has 4 character(s).   

* Word 16 has 5 character(s).   

* Word 17 has 3 character(s).   

* Word 18 has 4 character(s).   

* Word 19 has 3 character(s).   

* Word 20 has 4 character(s).

* Word 21 has 2 character(s).   

* Word 22 has 3 character(s).