-
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.
-
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)
-
Your suggestion did not work. I still get the same msgbox error when I run it.
-
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