I spent 5-6 hours today to try to get a working searchfunction for my textfile,
can someone please put me in the right direction?

I have this textfile, its a register over contacts:

Number Contacts = 3*
James Brown:555-12345:james@brown.com
Jack Yellow:555-42345:jack@yellow.com
Sally White:555-12345:sally@yahoo.com

For example, i want to search for "yahoo" and find the sally string.
I cant put a code since i have noone working or close to it.
However, my contacts is in the memory like this:

contact[0]=James Brown:555-12345:james@brown.com
contact[1]=Jack Yellow:555-42345:jack@yellow.com

..and so on.

The closest solution ive tried is "memchr" to compare contact strings with a searchstring,
but cant get it to work, tried to understand it but i dont. Im new to programming.
Maybe its a bad sollution anyway..

Recommended Answers

All 3 Replies

The strstr() function #include <string.h> will return whether or not a given string contains a given substring.

char string_to_search[] = "Hello, world!";
char string_to_find[] = "world";
char *pos;
pos = strstr( string_to_search, string_to_find );
if (pos != NULL)
  printf( "found it at index %d\n", (int)(pos -string_to_search) );
else 
  printf( "not found\n" );

Hope this helps.

I've started trying this function out, seems like i can use it!
I'll repost if (when) i get stuck for hours again ;-)

Thank you!

Yes! Got it working

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.