Thread: pattern search

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    99

    pattern search

    I am having problems with this code. It compiles fine but I get the illegal operations box ONCE now instead of three times as when I started this program two days ago. The message comes after the user has entered the pattern to search for.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define LENGTH 80
    
    static int str(char *a, char *b, int len)
    {
        for (;*a == *b; *a++, *b++)
        {
            if ( --len == 0)
                    return 1;
        }
        return 0;
    }
    
    static char *cmp( char *string, char *word, int len)
    {
        for (;*word != '\0'; *word++)
        {
            if ( str(string, word, len) == 1)
                    return word;
        }
        return NULL;
    }
    
    int main(int argc, char *argv[])
    {
        char *string, *word;
        int len, num,find=0;
        FILE *fd;
        
        if (argc < 2)
        {
            printf("Usage: TEXT <filename>");
            return 1;
        }
        
        if((fd=fopen(argv[1], "r"))==NULL)
        {
            printf("File Manipulation Error");
            return 1;
        }
        
        printf("Enter the search pattern: ");
        fgets( word, sizeof(word), stdin);
        num = strlen(word);
        
        while ( fgets( string, LENGTH, fd) != NULL)
        {
            if ( cmp( string, word, num) != NULL)
            {
                    printf("%s found\n", word);
                    ++find;
            }
        }
        
        if (find > 0)
            printf("%s found %d time(s)",word,find);
        
        return 0;
        }
    P.S. I know of the strstr() function. I am doing this for learning so please don't mention that function or one that does what I am doing.

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    char *string, *word;
    ...
    fgets( word, sizeof(word), stdin);
    ...
    while ( fgets( string, LENGTH, fd) != NULL)
    string and word are just pointers to char. You did not allocate any memory to store an input string. I guess you mean something like this:
    Code:
    char string[LENGTH] = {'\0'}, word[LENGHT] = {'\0'};
    ...
    fgets( word, LENGTH, stdin);
    ...
    while ( (fgets( string, LENGTH, fd)) != NULL)

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Your suggestion did not work. I still get the same msgbox error when I run it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You have quite a few problems with your code, but the big one is the cmp and str functions need a signifigant rewrite:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LENGTH 80
    
    static int str(char *a, char *b)
    {
      for (;*b != '\0'; a++, b++)
      {
        if ( *b != *a)
          return 0;
      }
      return 1;
    }
    
    static char *cmp( char *string, char *word)
    {
      for (;*string != '\0'; string++)
      {
        if ( str(string, word) == 1)
          return word;
      }
      return NULL;
    }
    
    int main(int argc, char *argv[])
    {
      char string[LENGTH], word[LENGTH];
      int find=0;
      FILE *fd;
      
      if (argc < 2)
      {
        printf("Usage: TEXT <filename>");
        return 1;
      }
      
      if((fd=fopen(argv[1], "r"))==NULL)
      {
        printf("File Manipulation Error");
        return 1;
      }
      
      printf("Enter the search pattern: ");
      fgets( word, sizeof(word), stdin);
      if ( word[strlen ( word ) - 1] == '\n' )
        word[strlen ( word ) - 1] = '\0';
      
      while ( fgets( string, LENGTH, fd) != NULL)
      {
        if ( cmp( string, word) != NULL)
        {
          printf("%s found\n", word);
          ++find;
        }
      }
      
      if (find > 0)
        printf("%s found %d time(s)",word,find);
      
      return 0;
    }
    Though if given the option, I would have recoded them as such:
    Code:
    static int str ( const char *a, const char *b )
    {
      while ( *b != '\0' ) {
        if ( *a++ != *b++ )
          return 0;
      }
      
      return 1;
    }
    
    static const char *cmp ( const char *a, const char *b )
    {
      while ( *a != '\0' ) {
        if ( str ( a++, b ) == 1 )
          return b;
      }
      
      return NULL;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 08:50 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM