Thread: need help writing search funtion

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    51

    need help writing search funtion

    I have wrote a database of char arrays and a search function for it, but I need to make it better. I have to allow it to accept '*' as a variable( any number of characters), and '?' as a variable( only one character ).

    If anyone has any ideas on how to do this it would be grealy appreciated. Thnx.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How good does it have to be?

    Simple, like DOS

    For example, if the * can appear only at the end (like match 'foo*'), then all you need do is detect the position of the '*' (use strchr), and then use strncmp instead of strcmp
    Code:
    char *p = strchr(seach,'*');
    if ( p ) {
      // it's got a *
      int pos = p - search;
      found = strncmp( line, search, pos );
    } else {
      // no *
      found = strcmp( line, search );
    }
    Intermediate, like this
    http://www.delorie.com/djgpp/v2faq/faq16_1.html

    Or advanced, like this
    http://www.cs.utah.edu/dept/old/texi...regex_toc.html

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Hey Salem, is there anyway I could send you my code so you can see exactly what I am doing? I would just post it, but it's for a school project and I really do want anybody to copy my code for it.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not really - this is an open forum for discussion and learning.

    Just post the relevant part - the function which takes a word, and a pattern, and returns true/false, depending on whether the word is matched by pattern (or not)

    Code:
    int match ( char *word, char *pattern ) {
    }
    match( "foobar", "foo*" );
    would be true

    match( "wibble", "zork" );
    would be false

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Ok, here is what I am searching with now.

    *********************************************
    printf("Enter the term you would like to search: ");
    gets(search_term);

    myclrscr();
    for ( i=0; i<total; i++ )
    {
    if ( (strstr(entry[i].fname, search_term)) || (strstr(entry[i].lname, search_term)) || (strstr(entry[i].phone, search_term)) )
    {
    printf("\nRecord:%d - %s, %s: %s", i+1, entry[i].lname, entry[i].fname, entry[i].phone);

    matches_found++;
    }
    }

    printf("\n\n\nThere were %d matches found. Press enter to return to the main menu. ", matches_found );

    getchar();
    flushall();
    *****************************************

    'total' is the amount of entries I have, so the for loop runs through testing each one. The if statement is checking each instance of the structure I use to hold the entries.

    I didn't quite understand how your first reply worked, and the first link you posted made it sound like that functionality was included with djgpp. Also, I have to be able to include the wildcards anywhere - So if an entry was Robert, I could find it by searching R*t or R?bert.

    Thanks for any help.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Sorry about the layout, I tried to indent everything but it didn't work.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you want to replace this
    if ( (strstr(entry[i].fname, search_term)) || (strstr(entry[i].lname, search_term)) || (strstr(entry[i].phone, search_term)) )

    With this
    if ( (match(entry[i].fname, search_term)) || (match(entry[i].lname, search_term)) || (match(entry[i].phone, search_term)) )

    Where match has the functionality as described above

    At the moment, you would have
    Code:
    int match ( char *word, char *pattern ) {
        return strstr( word, pattern ) == 0;
    }
    Then start to think about what it means to match against wildcards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search function not working fully
    By tabstop in forum C Programming
    Replies: 7
    Last Post: 12-04-2008, 02:57 AM
  2. Firefox and Google Search
    By DeepFyre in forum Tech Board
    Replies: 0
    Last Post: 01-16-2005, 10:28 AM
  3. Simple search program
    By colinuk in forum C Programming
    Replies: 6
    Last Post: 12-18-2004, 01:58 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Search Engines :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 06-18-2002, 12:31 PM