Yes Aia you are right that there are a few dumb mistakes in my code.

I did some searching and found the following code using strstr.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
void search_string(char *);
int main(void)
{
char array[SIZE];
puts("Enter a string:");
gets(array);
search_string(array);
char wait;
scanf( "%c", &wait );
return 0;
}
void search_string(char *array)
{
char *ptr, *strptr, word[SIZE] = {""};
int i = 0, j = 0;
ptr = array;
while (*ptr != '\0')
{
if (isspace(*ptr))
break;
word[i] = array[i];
ptr++;
i++;
}
strptr = array;
while ((strptr =strstr(strptr, word)) != NULL)
{
strptr++;
j++;
}
puts(array);
printf("%s occured %d times in the string\n", word, j);
}
I have tried to apply this code to my porblem but have been unsuccessful. Here is what I tried.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000
#define SIZE 100
void search_string(char *file);
int main()
{
char pattern[] = "of";
int counter = 0;
const char filename[] = "in.txt";
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
char line [ MAX ];
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
++counter;
printf( "%d ", counter);
fputs ( line, stdout ); /* write the line */
}
printf( "\n\n");
search_string(file);
fclose ( file );
}
else
{
perror ( filename );
}
char wait;
scanf( "%c", &wait );
return(0);
}
void search_string(char *file)
{
char *ptr, *strptr, word[SIZE] = {"of"};
int i = 0, j = 0;
ptr = file;
while (*ptr != '\0')
{
if (isspace(*ptr))
break;
word[i] = file[i];
ptr++;
i++;
}
strptr = file;
while ((strptr = strstr(strptr, word)) != NULL)
{
strptr++;
j++;
}
printf("%s occured %d times in the string\n", word, j);
}
However, when I run the code it outputs 0 as the number of times the word occurs.
I feel that by changing 'array' in the first code to 'file' in the second code I should have gotten the right answer because file, like array, is what was being searched for the string.
Does anyone see what I did wrong?