I have a text file which consists of about 30000 words. My goal is to count the actual number of the words (keep in mind that multiple punctuation marks and consecutive spaces are included, as well as words connected with -
(for example three-legged
), so counting just the spaces isn't correct).
I have managed to count the total characters but I am struggling with the words. Any help?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 50
char *getfile(void);
void stats(char *filename);
int main() {
char *file;
file = getfile();
stats(file);
return 0;
}
char *getfile(void) {
char *filename;
FILE *fp;
filename = malloc(SIZE);
printf("Enter the name of the text file: ");
scanf("%49s", filename);
fp = fopen(filename, "r");
printf("\n");
if (fp == NULL) {
printf("The entered file does not exist.");
printf("\n");
} else {
printf("The file exists.");
fclose(fp);
}
return filename;
}
void stats(char *filename) {
int cnt = 0, space = 0, lines = 0;
int c;
int count = 0;
FILE *fp;
fp = fopen(filename, "r");
while (((c = fgetc(fp)) != EOF)) {
cnt++;
if (c == ' ') {
space++;
}
if (c == '\n' || c == '\0') {
lines++;
}
}
printf("\nTotal characters in file: %d", cnt);
printf("\nTotal characters (excluding spaces) in file: %d", cnt - space);
fclose(fp);
return;
}