Thread: How find a substring in struct

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    How find a substring in struct

    Hi to All,

    I need to search the string occurencies in a structure like this:
    Code:
    struct node
    {
       char info[BUFLEN];
       struct node *next;
    };
     
    /* the word to search */
    char myWord[10];
    Can You suggest to me a sample, in order to resolve my problem ?

    Best Regards

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    What you need is the strstr() function. The prototype of the function is the following:
    Code:
    char * strstr ( const char *, const char * );
    In your case, you need to use it like this:
    Code:
    struct node myNode;
    
    // put some stuff into info
    
    if (strstr(myNode.info, myWord) != NULL)
    {
        puts("I found it!");
    }

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    26
    Quote Originally Posted by KONI View Post
    What you need is the strstr() function.
    Ok,
    But my problem is this:

    In the struct, each node can be for example
    "My oncle is the oncle of Martin"
    "My oncle is engeener"
    etc.

    I need to count the occurencies of word "oncle" in the entire struct. In my example are 3.

    How can perform this?

    By

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by jverkoey View Post
    like this.

    You iterate over all the list nodes, use strcmp() to see if the strings match and then increment a counter.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    If you want to search for the number of occurrences within a string, then you need to use strstr. Search for information about the function and you'll see that it returns a non-null char* that points to the exact location of the needle (the string you're searching for) in the haystack (the string you're searching in). Using this char* you can do another strstr to find the next occurrence (if there is one!).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  3. Replies: 11
    Last Post: 12-11-2005, 11:50 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. How to find a string in a struct..
    By Gugge in forum C Programming
    Replies: 3
    Last Post: 03-09-2002, 05:41 PM