0

I'm trying to write a program that gets a string, and counts how many times that string is found in a specific file.

The file is currently: hello my name hello is oria I like hello to program

And the word I'm counting is hello.

this is my code

int num_of_words(FILE* stream,char* str)
{
    int count=0,i=0,length;
    char c;
    rewind(stream);
    length=strlen(str);
    do
    {
        c=fgetc(stream);
        while(c==*(str+i))
        {
            c=fgetc(stream);
            i++;
            if(i==length)
            {
                count++;
                i=0;
            }
        }
        i=0;
    }while(c!=EOF);
    return count;
}

The idea is that there is a certain index called i, and it advances only if there is a match between letters. if i reached the length of the string, then it means we found all the letters in succession, and i raise count by one.

For some reason, it always returns zero.

Oria Gruber
  • 1,513
  • 2
  • 22
  • 44
  • 2
    `fgetc()` returns `int`, `EOF` is not a character value. – unwind Jun 13 '13 at 12:19
  • search string include newline. – BLUEPIXY Jun 13 '13 at 12:22
  • Do you need to know how many times a word is in a file, or how many time a string is in a file? For example, searching for the string "word" would get a hit in "Sword", is that what you want? If not then you might need regular expressions using the word boundary markers. C might not be the ideal language. – cdarke Jun 13 '13 at 12:24

2 Answers2

8

I. while (!eof) is wrong.

II. Don't reinvent the wheel - there's the strstr() function in the C standard library which helps you find a substring in another string.

I'd rather read the file into a buffer then use the following function. If the file is not too large, this should not be a problem.

int count(const char *haystack, const char *needle)
{
    int n = 0;
    const char *p = haystack;
    size_t len = strlen(needle);
    while (p = strstr(p, needle)) {
        n++;
        p += len;
    }

    return n;
}
Community
  • 1
  • 1
2

char c; should probably be int c; to accommodate for the type of data that fgetc returns. Otherwise, when char is an unsigned type c can never equal EOF and your loop will never end.

Before your code increments i once, it reads two characters from the file. That seems problematic to me. I'd move the c=fgetc(stream); in the inner loop to the end of that loop, eg:

    while(c==*(str+i))
    {
        i++;
        if(i==length)
        {
            count++;
            i=0;
            break;
        }
        c=fgetc(stream);
    }
autistic
  • 1
  • 3
  • 35
  • 80