Thread: Problem with search function within an ordered list

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    78

    Problem with search function within an ordered list

    This is the code:

    Code:
    struct libri
    {
    char name[MAX_LEN_TITLE];
    int next;
    }
     
    struct libri *start = 0;
    char book[MAX_BOOKS][MAX_LEN_TITLE];
    int s = 1;
     
    for(a = start; a; a = a->next, s++)
        {
            int i = 0;
            for(; title[i] != a->title[i]; i++);
            if(title[i] == a->title[i])
            {
                int num = 1;
                for(; title[i + num] == a->title[i + num]; num++);
                if(num == strlen(title))
                {
                    book[s] = a->title;
                }
                continue;
            }
        }

    The problem is: only if the titles of books in the database start with my search string, the function finds them (eg Title: hunting in the woods, Search string: hun).
    Any suggestions?
    Last edited by drew99; 09-02-2011 at 04:30 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you want a search to find book titles that DON'T match your query?

    You need to show some examples of what you DO want.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    78
    I want a search to find titles of books that also correspond in part to my query.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think you'll find that your second and third loops can go right off the end of the array.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    78

    RE

    The second too?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by drew99 View Post
    I want a search to find titles of books that also correspond in part to my query.
    1) You need to look into how for() loops work...
    2) You can't compare strings on the == or != token

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    78
    This is the complete function:

    Code:
    struct libri
    {
    int code;
    char title[MAX_TITLE_LEN];
    char des[MAX_DES_LEN];
    int next;
    }
    struct libri *start = 0;
    
    struct libri ***search(const char title[])
    {
        struct libri **books = (struct libri**) malloc(sizeof(struct libri*));
        if(!books)
        {
            puts("Ricerca interrotta");
        }
        books[0] = start;
        struct libri ***point;
        struct libri *a = (struct libri*)malloc(sizeof(struct libri));
        if(!a)
        {
            puts("Ricerca interrotta");
        }
        int s = 1;
        for(a = start; a; a = a->next)
        {
            if(strstr(a->title, title))
            {
                books = (struct libri **) realloc(books, sizeof(struct comp*) * (s + 1));
                if(!books)
                {
                    puts("Ricerca non completata");
                    goto ret;
                }
                point = &books;
                (*point)[s] = a;
                s++;
            }
        }
        (*point)[0]->code = s - 1;
    ret:
        return point;
    }

    I solved the previous problems, but two remain:
    - The books of the vector element 0 is equal to the element 1 (noticed when running the program)
    - If you start searching an empty database the statement "(* point) [0] -> code = s - 1," returns error.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sub word matching of a string, to all the titles ---hmmm.


    I'd use strstr() on every title, to start with, as a starting point.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Adak View Post
    I'd use strstr() on every title, to start with, as a starting point.
    Actually, it sounds like the title has to start with the search string, not just contain it:
    Quote Originally Posted by drew99 View Post
    The problem is: only if the titles of books in the database start with my search string, the function finds them (eg Title: hunting in the woods, Search string: hun).
    Any suggestions?
    If that's the case, then the OP needs to use strncmp(), not strstr(). However, it seems more useful to use strstr(), since many people don't consider small words like "the" and "a" when searching for a book title.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. insertion in an ordered list ?
    By jack_carver in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 11:11 AM
  2. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  3. Replies: 12
    Last Post: 01-28-2006, 07:40 AM
  4. problme with ordered linked list
    By palku in forum C Programming
    Replies: 5
    Last Post: 09-19-2005, 04:33 PM