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