find name problem

J

jwvai316

What should I do in the condition below?

I have an array called person

person[0].id = 111;
person[0].name = James Smith;
person[1].id = 222;
person[1].name = Paul Smith;
person[2].id = 333;
person[2].name = Billy Smooth;
person[3].id = 444;
person[3].name = Paul Gilbert;
person[4].id = 555;
person[5].name = John Gilbert;

and I want to make search program that takes input:

Input name search: Paul

and it will print

Name ID
---------- ---
Paul Smith 222
Paul Gilbert 444

and if the even if the input is "sm"
the progam will print

Name ID
---------- ---
James Smith 111
Paul Smith 222
BillY Smooth 333

and this search is case insensitive.

Thanks a lot for your answer.
 
D

David Resnick

jwvai316 said:
What should I do in the condition below?

I have an array called person

person[0].id = 111;
person[0].name = James Smith;
person[1].id = 222;
person[1].name = Paul Smith;
person[2].id = 333;
person[2].name = Billy Smooth;
person[3].id = 444;
person[3].name = Paul Gilbert;
person[4].id = 555;
person[5].name = John Gilbert;

and I want to make search program that takes input:

Input name search: Paul

and it will print

Name ID
---------- ---
Paul Smith 222
Paul Gilbert 444

and if the even if the input is "sm"
the progam will print

Name ID
---------- ---
James Smith 111
Paul Smith 222
BillY Smooth 333

and this search is case insensitive.

Thanks a lot for your answer.

Well, you can write the code and post it and people
here will generally be happy to provide assistance.

One idea on how to get started:
1) Convert the name to lower case (use tolower)
2) Convert the search string to lower case
3) Use strstr to search for the search string in the name

-David
 
M

Michael Mair

jwvai316 said:
What should I do in the condition below?

I have an array called person

person[0].id = 111;
person[0].name = James Smith;
person[1].id = 222;
person[1].name = Paul Smith;
person[2].id = 333;
person[2].name = Billy Smooth;
person[3].id = 444;
person[3].name = Paul Gilbert;
person[4].id = 555;
person[5].name = John Gilbert;

and I want to make search program that takes input:

Input name search: Paul

and it will print

Name ID
---------- ---
Paul Smith 222
Paul Gilbert 444

and if the even if the input is "sm"
the progam will print

Name ID
---------- ---
James Smith 111
Paul Smith 222
BillY Smooth 333

and this search is case insensitive.

Thanks a lot for your answer.

This sounds very much like a homework question.
Give us your best shot at the problem and we will help you.
Hint for the above: Have a look at tolower() (<ctype.h>) and
strstr() (<string.h>) and maybe isspace() (also <ctype.h>)


Cheers
Michael
 
T

Thomas Matthews

jwvai316 said:
What should I do in the condition below?

I have an array called person

person[0].id = 111;
person[0].name = James Smith;
person[1].id = 222;
person[1].name = Paul Smith;
person[2].id = 333;
person[2].name = Billy Smooth;
person[3].id = 444;
person[3].name = Paul Gilbert;
person[4].id = 555;
person[5].name = John Gilbert;

and I want to make search program that takes input:

Input name search: Paul

and it will print

Name ID
---------- ---
Paul Smith 222
Paul Gilbert 444

and if the even if the input is "sm"
the progam will print

Name ID
---------- ---
James Smith 111
Paul Smith 222
BillY Smooth 333

and this search is case insensitive.

Thanks a lot for your answer.

Take a look at the functions:
qsort
bsearch
You need to create a function which will
return an ordering value (-1, 0, +1) given
two items.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
E

Eric Sosman

jwvai316 said:
What should I do in the condition below?

I have an array called person

person[0].id = 111;
person[0].name = James Smith;
person[1].id = 222;
person[1].name = Paul Smith;
person[2].id = 333;
person[2].name = Billy Smooth;
person[3].id = 444;
person[3].name = Paul Gilbert;
person[4].id = 555;
person[5].name = John Gilbert;

and I want to make search program that takes input:

Input name search: Paul

and it will print

Name ID
---------- ---
Paul Smith 222
Paul Gilbert 444

and if the even if the input is "sm"
the progam will print

Name ID
---------- ---
James Smith 111
Paul Smith 222
BillY Smooth 333

You could use the strstr() function in a loop,
checking to see whether your input fragment appears
anywhere in each name:

char key[] = "Paul";
for (i = 0; i < NPERSONS; ++i) {
if (strstr(person.name, key) != NULL)
... print person neatly ...
}
and this search is case insensitive.

Aww, ratz! I *knew* it was too easy: strstr() is
case-sensitive. Three possibilities, from easiest to
hardest:

- Check your system's documentation to see whether
it provides a case-insensitive equivalent to strstr().
(The Standard C library offers no such function, but more
than one system provides beyond-the-Standard extras.)
If such a thing exists on your system (and you don't care
whether your program will work on any other systems), go
ahead and use it.

- Convert both the search key and the stored name to
upper (or lower) case using the <ctype.h> facilities, then
search as above but using the transformed data. If the
searches are infrequent you could convert each name "on
the fly" as you search for it (using a temporary copy so
as not to destroy the original properly-cased name); if
the searches are frequent it might be better to store both
the original and the converted names in the array:

person[0].id = person[0].id = 111;
person[0].name = "James Smith";
person[0].lcname = "james smith";

- Write your own case-insensitive variant of strstr().

The above suggestions are only a few ways of tackling
the problem; many others exist. You'll definitely want to
use a fancier approach if NPERSONS is in the millions.
 
C

CBFalconer

jwvai316 said:
What should I do in the condition below?

I have an array called person

person[0].id = 111;
person[0].name = James Smith;
person[1].id = 222;
person[1].name = Paul Smith;
person[2].id = 333;
person[2].name = Billy Smooth;
person[3].id = 444;
person[3].name = Paul Gilbert;
person[4].id = 555;
person[5].name = John Gilbert;

and I want to make search program that takes input:

Input name search: Paul

and it will print

Name ID
---------- ---
Paul Smith 222
Paul Gilbert 444

and if the even if the input is "sm"
the progam will print

Name ID
---------- ---
James Smith 111
Paul Smith 222
BillY Smooth 333

and this search is case insensitive.

We do homework at $200 (US) per hour, 4 hour minimum, payment for
the first 4 hours in advance. We also require the name and email
address of your instructor so we can deliver the result directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,897
Members
47,439
Latest member
shasuze

Latest Threads

Top