// COUNTS THE LINES, WORDS AND CHARACTERS IN A TEXT FILE #include #define YES 1 #define NO 0 main(int argc, char *argv[]) { int c, nl, inword, es, uc; long nc, nw, ns; char *s; FILE *fh; inword = NO; nc = nw = ns = nl = 0; fh = fopen( s = *++argv, "r" ); if (fh == NULL) printf("File not found."); else { while((c = getc(fh)) != EOF) { ++nc; if(c == '\n') ++nl; /* We are in whitespace, so reset the inside-a-word flag and if the previous char was a full stop and one before that not a capital, increment the sentence count and reset the end-of-sentence flag. */ if(c < 33) { inword = NO; if(es == YES && uc == NO) { ns++; es = NO; } } /* Else we are inside a word, so, if the 'inword' flag is false, set it true and increment the number of words encountered. If we have reached the end of a sentence, set the 'end of sentence' flag. Else we are not at the end of a sentence yet, so if this letter is not upper case, clear the upper-case flag else set it. */ else { if(inword == NO) { inword = YES; ++nw; } if(c == '.' || c == '?' || c == '!') es = YES; else if(c < 'A' || c > 'Z') uc = NO; else uc = YES; } } printf("%s has %ld chars, %ld words, %ld sentences, %d lines\n", s, nc, nw, ns, nl); printf("N§ of words per sentence = %d\n", nl = nw / ns); fclose(fh); } } // LISA MORTON, CHAPMAN 106, SOUTHLANDS