0

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..

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Fredszky
0

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.

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.