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

Write a function (WC .m) that takes as input a string containing the filename of

ID: 3832784 • Letter: W

Question

Write a function (WC .m) that takes as input a string containing the filename of a text file and returns three values: the number of lines, number of words, and number of characters in the file. Here is a self-referential test file (the answers are in the text file itself): 1| this file has 5 lines 2| and 15 words and 3| 77 characters. 4| 5| this line counts, too. The line numbers on the left side are for reference. The file itself is on eCampus. By now, you should know that you must think before you code^1. Take a few minutes right now to draw out your plan (as pseudocode or a flow diagram). If you do this, you will make your instructor happy. If you don't, you will make them sad. Please don't make your instructor sad.

Explanation / Answer

Here is the code for you:

function [numOfLines, numOfWords, numOfChars] = WordCount(fileName)
fin = fopen(fileName, 'r');
numOfLines = 0;
numOfWords = 0;
numOfChars = 0;
while ~feof(fin)
numOfLines = numOfLines + 1;
%numOfWords = numOfWords + 1;
line = fgets(fin);
numOfChars = numOfChars + length(line);
for i = 1 : length(line)
if(line(i) == ' ')
numOfWords = numOfWords + 1;
numOfChars = numOfChars - 1;
end
end
end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote