string pattern search problem

This is a discussion on string pattern search problem within the C Programming forums, part of the General Programming Boards category; I'm having a problem with finding a substring count for finding a string of characters in a text file. For ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    string pattern search problem

    I'm having a problem with finding a substring count for finding a string of characters in a text file. For example, if the user enters the pattern 'the' to search for then the following words in a text file should return true: 'the', 'mother', 'then', 'their' but not 'The'.

    My problem is that if the user types in a string such as 'net' to search and if a pattern of 'net' characters appears more than once, it is only counting some of the occurrences of 'st' in the file and not all of them. That particular pattern occurs almost 15 times in the attached data file that I've included with this post (data.txt) yet it only returns a count of 6.

    For example, what should be happening is if the user were to enter a search of 'st', I'd need a method of finding the string of 'st' not only in words such as 'street' or 'against' where the string occurs at the start or end of the word but also in words such as 'christmas' where 'st' occurs right in the middle of a string too.

    Here is the code I have so far:

    Code:
    FILE *fp;
    fp = fopen("data.txt", "r");
    char temp[256]; // reads a line from the file
    char *searchStr;
    int found = 0;
    printf("Enter a string to search for : ");
    scanf("%s", *&searchStr);
    
    while (fgets(temp, 256, fp) != NULL) {
        if ((strstr(temp, searchStr)) != NULL) {
              found++;
        }
    }
    
    printf("\n\nThe string %s appeared %d times in the file", searchStr, found);
    If anyone has advice or suggestions, feel free to post them. Any help would be greatly appreciated. I've included my own sample text data file which I created for reference.
    Attached Files Attached Files

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    scanf("%s", *&searchStr);
    Now that's a creative new attempt at undefined behavior. FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Re:

    yeah I should have used

    fgets(searchStr, sizeof(searchStr), stdin);

    to get the search word from the user. Even with that, it still doesn't work.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    of course it doesn't, you have defined searchStr as a pointer. Only few functions will allocate memory for you, you know...
    anyway, you have also to check for '\n' (too small buffer?) and replace it by '\0' after fgets call. Also your search is uh... simple. Here:
    Code:
    char *t = temp;
    while ((t = strstr(t, searchStr)) != NULL)
    {
        t += length_of_searchStr;
        found++;
    }
    And if I would do it, I wold have readed _whole_ file into memory and then searched it.
    Last edited by iwabee; 11-25-2004 at 06:30 AM.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Re:

    I tried your method but still couldn't get it to work. I included the newline character search and making it a null terminator. Here is what I have now:

    Code:
    char *t;
    char word[256]; // user entered search word
    char temp[256]; // text from file
    int found = 0;
    
    printf("Enter a string to search : ");
    fgets(word, sizeof(word), stdin);
    
    find = strchr(word, '\n');
    
    if (find) {
       *find = '\0';
    }   
                    
    t = temp;
    
    while ((t = strstr(t, word)) != NULL) {
                          t += strlen(word);
                          found++;
    }         
    
    printf("\n\nThe string %s appeared %d times in the file", word, found);
    The main problem now is the count equals zero for every search I make which is totally wrong.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Re:

    I forgot to include a line of code in my last post:

    Code:
    char *t;
    char *find;
    char word[256]; // user entered search word
    char temp[256]; // text from file
    int found = 0;
    
    printf("Enter a string to search : ");
    fgets(word, sizeof(word), stdin);
    
    find = strchr(word, '\n');
    
    if (find) {
       *find = '\0';
    }   
                    
    t = temp;
    
    while ((t = strstr(t, word)) != NULL) {
                          t += strlen(word);
                          found++;
    }         
    
    printf("\n\nThe string %s appeared %d times in the file", word, found);

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    thinking is allowed...
    Code:
    while (fgets(temp, 256, fp) != NULL)
    {
        char *t = temp;
        while ((t = strstr(t, searchStr)) != NULL)
        {
            t += length_of_searchStr;
            found++;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21