1

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.

user12205
  • 2,684
  • 1
  • 20
  • 40
user3441417
  • 45
  • 2
  • 9
  • fscanf returns the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure. You are comparing an int value with '.' . – jahan May 08 '14 at 09:22
  • Note that you should use `fgets(word, sizeof(word), stdin);` instead of `gets(word);` to avoid buffer overflow. – user12205 May 08 '14 at 14:11

3 Answers3

2

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.
Community
  • 1
  • 1
M Oehm
  • 28,726
  • 3
  • 31
  • 42
1

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

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • 1
    There's no need to watch for double spaces - `strtok` regards a sequence of delimiter characters as a single delimiter. – M Oehm May 08 '14 at 09:26
  • Sorry im still a noob especially with strings ,Ive never used strstr how would i use it to do this and im not sure how you would format it to read the variable word – user3441417 May 08 '14 at 09:28
1

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!

user12205
  • 2,684
  • 1
  • 20
  • 40
jahan
  • 521
  • 1
  • 4
  • 15
  • 2
    Your program will not find any sentences unless the scannef file always separates the punctuation from the words with a space. Usually, the full stops have no space between the word they precede, so you probably shouldn't compare the string to `"."` but check whether it contains a full stop instead. – M Oehm May 08 '14 at 09:55
  • Yea you are right . Thank you for taking the time to answer this – user3441417 May 08 '14 at 10:19
  • 1
    Note that you should use `fgets` instead if `gets` – user12205 May 08 '14 at 14:08
  • where ? I am reading from stdin. I think I do not have to use fget. – jahan May 08 '14 at 14:10
  • 2
    See http://stackoverflow.com/q/1694036/3488231 or http://stackoverflow.com/q/4309746/3488231 – user12205 May 08 '14 at 14:14