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;
}