0

The goal is basically to recreate wc. I need to count words, characters, non white space characters and new lines. I have everything except words I can't figure out how to make it work.

When I searched here everyone was under the assumption that the document did not have multiple white spaces between words. The documents I have to test with are guaranteed to have multiple spaces so this method for counting words does not work.

#include <stdio.h>

int main (int argc, char* argv[]) {
  int Spaces;
  Spaces = 0;
  int NewLine;
  NewLine = 0;
  int Characters;
  Characters = -1;
  char* filename = argv[1];

  if (argc < 2) {
    printf("Usage: \n   wc <filename>\n");
  } else {
    printf("Filename is: %s\n", filename );
    FILE* infile;
    infile = fopen(filename, "r");

    char c;
    do {
      if (c == ' ') {
        Spaces = Spaces + 1;
      }
      if (c == '\n') {
        NewLine = NewLine + 1;
      }
      Characters = Characters + 1;
    } while ((c = fgetc(infile)) != EOF);

    printf("Total number of characters: %d\n", Characters);
    Characters = Characters - NewLine - Spaces;
    printf("Total number of non-whitespace characters: %d\n", Characters);
    printf("Total number of lines: %d\n", NewLine);
  }
  return 0; 
}
fenceop
  • 1,439
  • 3
  • 18
  • 29

2 Answers2

1

Normally you use a Boolean variable, typically named something like in_word, which you set to false if the current character is white-space, and true if it's not. You increment the word count when (and only when) it changes from true to false (or vice versa).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Implement your code as having two states: in-a-word and not-in-a-word. Then increment your count when transitioning between states.

I suggest incrementing word count when transitioning from not-in-a-word to in-a-word (as opposed to in-a-word to not-in-a-word), so no special processing will be needed at the end of the file.

the paul
  • 8,972
  • 1
  • 36
  • 53
user3629249
  • 16,402
  • 1
  • 16
  • 17