سؤال

I have to write a program for an assignment to scan a text file and say how many sentences there are (based on number of full stops, no other punctuation) the number of words in total and the total number of times a certain word appears. I can get the count of total words working but the specific word count and sentence count elude me, I believe we are meant to use fscanf. Here is what I have

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
FILE *datafile;
int main(){
    int count1, count2, leng, sentences ;
    char filename[50], word[120], sentence[120];
    printf("Enter a word :");
    gets(word);
    printf("Enter the filename:");
    gets(filename);
    datafile = fopen(filename, "r");
    if(datafile == NULL) {
        printf("Error , Can not open %s", filename);
        exit(249);
    }

    leng = 0;
    count2 = 0;
    sentences = 0;

    while (fscanf(datafile, "%s", word) == 1) {     //this one works
        ++leng;
    }
    while (!feof(datafile))
    { 
        fscanf(datafile,"%s",word);
        if (strcmp(word,"the")==0)
            count2++;
    }

    while(fscanf(datafile, "%s", word) == '.') {
        ++sentences;
    }
    printf("There are a total of %d words\n", leng);
    printf("The word '%s' appears %d times\n", word, count2);
    printf("There are %d sentences", sentences);

}

when I run it this is what I get

I tried two different ways for the sentence count and specific word count and i dont really understand why the first one works either , I imagine once I have one of them the other will be the same format . Any help or advice is appreciated.

هل كانت مفيدة؟

المحلول 2

The complete program is here:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
FILE *datafile;
int main(){
    int count1, count2, leng, sentences ;
    char filename[50], word[120],word1[120] ,sentence[120];//word1 is new string
    printf("Enter a word :");
    gets(word1);
    printf("Enter the filename:");
    gets(filename);
    datafile = fopen(filename, "r");
    if(datafile == NULL) {
        printf("Error , Can not open %s", filename);
        exit(249);
    }

    leng = 0;
    count2 = 0;
    sentences = 0;

    while (fscanf(datafile, "%s", word) == 1) {     //this one works
        ++leng;

        if (strcmp(word,word1)== 0)//strcmp returns 0 when it matches word to word1
            count2++;

        if ( strchr(word,'.') != NULL )//strchr returns a pointer when it matches word to '.' null otherwise
            ++sentences;
    }

    printf("There are a total of %d words\n", leng);
    printf("The word '%s' appears %d times\n", word1, count2);
    printf("There are %d sentences", sentences);

    return 0;
}

Have a look of various string functions and there return values. Here!

نصائح أخرى

There are several issues with your code.

  • You read your file in three consecutive while loops. The second and third loops are never entered, though, because after scanning the text with fscanf, the file is exhausted. You could rewind your file before re-reading it, but it is probably better to do all three counts in one pass.
  • You use the variable word for all kinds of things: as a word the user specifies and as temporary buffer for the words you scan. This is why your screenshot shows "1972." as the word to look for, not the "of" the user entered. (And when counting that word, you check for the hard-coded word "the".)
  • The return value of fscanf is the number of % items sucessfully matched or the special value EOF that indicated that the end of the file has been reached. You already use this correctly in your first loop. Please don't add an additional check with feof, because it doesn't behave as most pleople think it should, see this self-answered question.
  • The way you use fscanf when looking for a full stop is syntactically correct, because it returns an int and the ASCII value for the dot '.' in an int, but is is not what you want. The fscanf with %sformat returns a string, not a single character. Also note that fscanf does not have the same notion of sentences and punctuation we have: A full stop is likely to be sitting at the end of the string as in "1972." in your screenshot. You should therefore check whether the word has a full stop. You can do that with strchr.

I am giving you a few hints:

To get sentence count, you can use strtok with delimitter as "." (full-stop).

To get word count, use strtok with delimitter as " " (space).

To get word repetition count you can use strstr and search for that specific word.

UPDATE: To use strstr, you can read the file content (line-by-line) into a buffer string using fgets and then use strstr to find a specific word in that sentence/string, then again read the next sentence into buffer string (again using fgets), and so on till you reach End-Of-File.

Read more about these functions at: fgets, strstr, strtok

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top