0
while( (ch = fgetc( infile )) != EOF )
    if(ch ==' ') words++;

It works nice, but in case if we have blank lines in a string, how are we suppose to detect these lines and to count thw words right?

Maroun
  • 94,125
  • 30
  • 188
  • 241
July
  • 15
  • 1
  • 1
  • 7
  • Use `isspace` instead of `ch==' '`. It considers new-line to be whitespace. Then you'll need to fix your logic so it doesn't try to count a run of (say) four spaces as four words. – Jerry Coffin Dec 15 '14 at 17:40
  • What do you mean by blank lines? Several blanks in a row? – Philipp Murry Dec 15 '14 at 17:40
  • Also, what if the string doesn't have a space at the end? Your code currently counts "Hello World!" as one word. – slugonamission Dec 15 '14 at 17:41
  • possible duplicate of [Counting words in a string - c programming](http://stackoverflow.com/questions/12698836/counting-words-in-a-string-c-programming) – Philippe Signoret Dec 15 '14 at 17:41
  • 1
    slugonamission,I skipped most of the code,so in the beginning,Ideclare the counter as 1 – July Dec 15 '14 at 17:43

3 Answers3

6

Your code does not count words, it counts spaces. In many cases the two counts would be different - for example, when words are separated by more than one space.

You need to change the logic in such a way that you set a boolean flag "I'm inside a word" when you see a character that belongs to a word, and has the following logic when it sees a whitespace character (a space, a tab, or a newline character):

if (isspace(ch)) {
    if (sawWordFlag) {
        words++;
        sawWordFlag = false;
    }
}

One way to detect if a character belongs to a word is to call isalnum on it. Both isalnum and isspace functions require you to include <ctype.h> header.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

So sscanf already does what you need, it will eat any number of whitespaces before a string including tabs and newlines. This algorithm works with leading or trailing spaces as well.

int words = 0;
int i = 0;

while(sscanf(inFile, "%*s%n", &i) != EOF){
    inFile += i;
    words++;
}

sscanf is extremely versatile you can easily read out each word as follows:

int words = 0;
int size = strlen(inFile);

if(size > 0){
    char* word = (char*)malloc((size + 1) * sizeof(char));

    for(int i = 0; sscanf(sentence, "%s%n", word, &i) > 0; sentence += i){
        // Do what you want with word here
        words++;
    }
    free(word);
}
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
char prev = 'x'; // anything but space
while((ch == fgetc(infile)) != EOF)
{
    if(ch == ' ' && ch == prev)
        continue;
    else if(ch == ' ' && ch != prev)
        words++;
    prev = ch;
}
Dan
  • 1,874
  • 1
  • 16
  • 21
  • 2
    This solution will report incorrect results for files that have a single word per line ([demo](http://ideone.com/HvQRex)), even if you fix the obvious error in the loop header. – Sergey Kalinichenko Dec 15 '14 at 18:04
  • 1
    Also reports incorrectly with leading and trailing spaces: " x " reports. And cannot handle newlines as requested by the OP. – Jonathan Mee Dec 15 '14 at 19:16