In: Computer Science
Lab 08: Finding Nemo
Youshouldturnin:1.A file named Lab08.m, and a screenshot of you running findingNemo() from thecommand line.
Instructions:1.On Canvas you will find a file named findingNemo.m and another named nemo.txt. Thefirst contains code which will read in strings from nemo.txt, then call Lab08(string) oneach line of nemo.txt. Your job is to write Lab08.m.
2.The Lab08 function will take in a string as input and will return an integer n as output.
3.The value of n will be the nth word in the input string, where n is the location of thesubstring 'Nemo'.a.Please note that you will need to match whole words only, so 'aNemone' and'Leonard Nemoy' should not be countedb.All strings will end with punctuation, so you do not need to work out the edgecase of a string like 'I found Nemo', which could cause significant problems
4.Once the value of n has been determined (hint: count the number of spaces before theindex at which you found Nemo), use the statement fprintf('I found Nemo at word%d!\n',n); to print the results.
5.If Nemo is not in the string, instead use the statement fprintf('I couldn''t find Nemo,sorry.\n'); and set n equal to negative one.
6.If Nemo is in the string multiple times, return the location of its first appearance.
7.Please be aware that if your file and function names are not the same as those expectedin findingNemo(), or if all three files are not in the same folder, your code will not workcorrectly.
function [] = findingNemo()
fileID = fopen('nemo.txt','r');
while true
line = fgets(fileID);
if line == -1
break;
end
n = Lab08(line);
fprintf('n = %d\n',n);
end
end
The author of this assignment swears she has never seen the movie Finding Nemo.No, for real, this is totally a joke about Captain Nemo from 20,000 Leagues Under the Sea.Okay, fine, I've never read 20,000 Leagues. But I did see Nadia: The Secret of Blue Water, which has a character named Captain Nemo.Or maybe it's NEMO: Never Eat More Oreos.I'll ask Leonard Nemoy. Or is it Nimoy?Wait, those don't match Nemo.At this point, Nemo doesn't even sound like a word anymore.I guess it's not, Nemo is a name.Buffalo buffalo buffalo buffalo. Nemo Nemo Nemo?Is it spelled Nemotode or Nemotoad? Oh well, let's study anemone instead.I'm thinking about making a cryptocurrency for Nebraska. What do you think of the name NEMoney?Nemo is also a town in South Dakota, evidently.Apparently NVIDIA has a toolkit for creating AI called NeMo, so that's weird.I'm running out of jokes about the name Nemo, to be honest.Let's just go watch Finding Nemo.
Lab08 function is given below.
Note: line starting with '//' are comment lines. To explain in detail, comment lines were included.
function [] = Lab08(line)
n = 0
// get the string length
len = strlen(line);
// each word is seperated by space
oneSpace = ' '
while true
// n1 - temperory variable
n1 = n;
while n1 < len
// get single character at index n
ch = getch(line, n1)
// Check whether the character is space, if it is space, return the index
if ch == ' '
break;
// increment the index
n1 = n1 +1
end
// index not found
if n1 >= len
n1 = -1
if n1 == -1
break;
// extract the word between two spaces
word = substr(line, n, (n1-n))
// trim function is used to trim the punctuation and space in the beginning and end of the word
word = trim(word)
// if the word is not Nemo set the n to -1.
if word != "Nemo"
n = -1
// if the word is not Nemo, print the message
if n < 0
fprintf('I couldn''t find Nemo,sorry.\n');
// if the word is Nemo, print the message
if n >= 0
fprintf('I found Nemo at word%d!\n',n);
// Set the next index
n = n1
end
end