list of telephone numbers

2 次查看(过去 30 天)
Britnie Casillas
Britnie Casillas 2019-11-8
评论: Jeremy 2019-11-8
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 个评论
Britnie Casillas
Britnie Casillas 2019-11-8
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 2019-11-8
You could use
strtok
to break apart the names, if it is always <firstname lastname> then
[firstname,s] = strtok(tline);
[lastname,s2] = strtok(s);

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by