Clear Filters
Clear Filters

list of telephone numbers

6 views (last 30 days)
Britnie Casillas
Britnie Casillas on 8 Nov 2019
Commented: Jeremy on 8 Nov 2019
I am trying to create a code that reads a file and search for a particular name and display the line with the first, last name, and the phone number
the file is:
Arthur Jones (365)271-8912
John Smith (011-44-235)135246
Simon Addison (699)987-6543
Rachel Jones (444)361-8990
Jean-Paul Maronne (011033-1)3482152
Hideo Takata (011-81-3)3456-1212
and my code is:
function phonebook(last, first)
fid = fopen('list_of_telephone_numbers.txt', 'r');
if fid == -1
disp('File not opened successfully')
else
while feof(fid)==0
tline = fgetl(fid);
a = strfind(tline, last);
b = strfind(tline, first);
if ~isempty(a) && ~isempty(b)
disp(tline)
end
end
end
end
In the command window, when I type
>>phonebook('Jones', 'Arther')
I get blank results
>> phonebook('Jones', 'Arther')
>>
  3 Comments
Britnie Casillas
Britnie Casillas on 8 Nov 2019
Awkward... I didnt realize I was spelling the name wrong...
It worked when I spelled it right. So, how do you differentiate between the surname and forename?
Jeremy
Jeremy on 8 Nov 2019
You could use
strtok
to break apart the names, if it is always <firstname lastname> then
[firstname,s] = strtok(tline);
[lastname,s2] = strtok(s);

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!