My program has to count the amount of times the word given as an argument to main is given by the user. This all worked untill I added the bit where the program has to exit when EOF is the input.
Here is my file
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(int *p, int *q);
int main(int argc, char *argv[])
{
char buf[1026] = {0};
char *p = buf;
int cnt = 0, i;
const char *word = argv[1];
int length_arg = strlen(argv[1]);
while (fgets (buf, 1025, stdin) != "EOF")
{
for(i = 0; fgets (buf, 1025, stdin) != NULL; i++)
{
for (i = 0; i < 1025; i++)
{
if (strncmp(p + i, word, length_arg) == 0)
cnt++;
}
}
printf("%d", cnt);
}
printf("%d", cnt);
return 0;
}
the input is:
Let it be, let it be, let it be, let it be.
Whisper words of wisdom, let it be.
EOF
The outcome should be (my argument to main is "let")
4
Can someone help me out?