Instructions:
findingNemo.m:
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
Nemo.txt:
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.
SOLUTION :
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
Get Answers For Free
Most questions answered within 1 hours.