{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

dictionary - while ischar(ln%sets up condition ln =...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
function def = dictionary(file,word) % Given the name of a .txt file and one word you are looking for, return % the definition of the word. All dictionary entries in the text file % will be on one line, and the word and its definition will be separated % by a dash. See dictionary.txt for further details. Your definition % should not have leading dashes or spaces before it. If the entry does % not exist, the function should output 'Entry Not Found'. % Dictionary entries will always be in the same format as dictionary.txt. % Your word search should be case sensitive. %usage: def = dictionary(file,word) fh = fopen(file,'r'); %opens the file to read into it ln = ''; %creates an empty string to start while loop
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: while ischar(ln) %sets up condition ln = fgetl(fh); %sets up terminating condition index = findstr(ln,word); %finds which line the specific word is in if ~isempty(index) %if the index is not empty, this will run [token def] = strtok(ln,'-'); %tokenizes the line where the word is found [def rest] = strtok(def,'-'); %tokenizes again to get the definition by itself if def(1)==' ' %finds the first position in the definition def(1) = ; %removes the space break; %gets out of loop once this is met end end end if isempty(index) %sets up def if the word is not in the text file def = 'Entry Not Found'; end fclose(fh); end e...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online