-2

I have a file named myf which has a lot of text in it and I am trying to use blank spaces as a way of counting the number of words. Basically, in the count method of my program, there is a variable int d which acts like a boolean function. Also, there is an incrementer called count.

I have a for loop which will traverse the array that's put into the argument of the method count, and will see if the pointer *p is a non letter. If it is a non letter AND d=0, d=1 and count is incremented. This way, if the next character is also a non space, since d=1, the else if statement will not be incremented again. The only way for d to reset to 0 is if a space is present, at which point, if another letter is found, it will be incremented again. Then the method count will return the variable count. Seems simple enough, but I keep getting wrong numbers.

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>

int count(char x[]) {

  int d = 0;
  int count = 0;

  for (char *p = x; *p != EOF; *p++) {
    // this will traverse file
    printf("%c", *p);

    // this is just to see the output of the file
    if (*p == ' ' && d == 1) {
      d = 0;
    }

    else if (*p != ' ' && d == 0) {
      count++;
      d = 1;
    }
  }

  return count;
}

int main() {

  char c;
  int r = 0;
  char l[1000];

  FILE *fp = fopen("myf", "r");
  while ((c = fgetc(fp)) != EOF) {
    l[r] = c;
    r++;
  }

  printf("\n %d", count(l));
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Bossrevz
  • 33
  • 1
  • 4
  • I'm not too familiar with C but can you just read your file into a big long string and split it on a space and then get the `array.Length` property? – Radmation May 02 '16 at 21:45
  • But your best friend is a code formatter. Single spacing a rational indentation are really beneficial. – William Pursell May 02 '16 at 21:45
  • it works for single lines, but when i have texts containing 60 words itll undercount it :/ – Bossrevz May 02 '16 at 21:45
  • You might want to count newlines as spaces as well... – Eugene Sh. May 02 '16 at 21:48
  • @Eugene Sh. GENIUS. that worked. Now i need an explanation on why my array is messed up at the end of the file. contents within the file: https://gyazo.com/d7a6b3bd1fd5ae843d5d427e72980f28 output of the file from array: https://gyazo.com/bdc1d73827ae61d80fc3a3e203a3c987 – Bossrevz May 02 '16 at 21:54
  • It does not end with a null character. try `l[r] = '\0';` after while-loop. Also `char c;` change to `int c;` – BLUEPIXY May 02 '16 at 21:59
  • @BLUEPIXY GENIUS. THANK YOU. GENIUS GENIUS GENIUS. – Bossrevz May 02 '16 at 22:05

2 Answers2

2

To count the number of words, count the occurrences of a letter after a non-letter.

*p != EOF is the wrong test. EOF indicate that the input operation either 1) had not more input or 2) an input error occurred. It does not signify the end of a string.

Use int to save the result from fgetc() as that returns an int in the range of unsigned char and EOF. Typically 257 different values. char is insufficient.

Small stuff: No need for an array. Let code consider ' as a letter. As the number of words could be very large, let code use a wide type like unsigned long long.

#include <ctype.h>
int isletter(int ch) {
  return isalpha(c) || c == '\'';
}

#include <stdio.h>
int main(void) {

  unsigned long long count = 0;
  FILE *fp = fopen("myf", "r");
  if (fp) {
    int c;
    int previous = ' ';
    while ((c = fgetc(fp)) != EOF) {
      if (!isletter(previous) && isletter(ch)) count++;
      previous = ch;
    }
    fclose(fp);
  }    
  printf("%llu\n", count);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Don't do this

*p != EOF

EOF is actually a negative integer and you're using it as a char. You should pass in how many character you want to iterate over ie

int count(char x[], int max){

then use the for loop like

int m = 0;
for ( char *p = x; m < max; p++, m++) 

Note I also changed *p++ to p++. You also need to update your program to consider things that are non space etc ie this line

else if (*p != ' ' && d==0 )

What happens when it encounters a \n, it will likely count an extra word.

Harry
  • 11,298
  • 1
  • 29
  • 43
  • is this why my file output has a string of random characters at the end of a file as shown in this picture? https://gyazo.com/bdc1d73827ae61d80fc3a3e203a3c987 https://gyazo.com/d7a6b3bd1fd5ae843d5d427e72980f28 – Bossrevz May 02 '16 at 21:56
  • You cannot use EOF the way you're using it. I'll udpate my answer. – Harry May 02 '16 at 22:02