Searching for and counting how many times does a word appear in the textfile in C

53 views Asked by At

I have this program here to search for and count how many times a certain word appears inside a string from textfile.

for(int i = 0; i < file_string_lenght - word_search_lenght; i++)
    {
        found = 1;
        for(int j = 0; j < word_search_lenght; j++)
        {
            if(file_string[i + j] != word_search[j])
            {
                found = 0;
                break;
            }
        }
        if(found == 1)
        {   //the question on the start of the program, whether user wants to print each found word's location
            if(strcmp(response, "yes") == 0){
                printf("'%s' found at index: %d \n", word_search, i);
            }
            count_of_word_search += 1;
        }
    }
    if(pocet == 0){
        printf("The word '%s' was not found in the file.", word_search);
        printf("\n\n");
        system("pause");

        return 0;
    }
    printf("\nNumber of '%s' in the file: %d", word_search, count_of_word_search);

How can I do so, that for example the word 'day' doesn't count if it's in 'today' or 'yesterday'. I understand that it needs to recognize a dot, column or a space betweeen the word I'm searching for and the rest of the string. But I've yet to come to a way how to implement this into this kind of algo. Thanks for help

0

There are 0 answers