/* * Program to find "words" (sequences of "isalpha" characters). * * Input is a text file. * * Output is a text file with the "words" from the input file, * in lowercase, one per line. * * Names for the two files are given as command-line arguments. * * Also print (to stdout) totals of alphabetic characters and all * characters. */ #include #include #include int main(int argc, char *argv[]) { if (argc < 3) { printf("usage %s infile outfile\n", argv[0]); return 1; } FILE * infile = fopen(argv[1], "r"); if (infile == NULL) { printf("cannot open input file\n"); return 1; } FILE * outfile = fopen(argv[2], "w"); if (outfile == NULL) { printf("cannot open output file\n"); return 1; } int inchar; int count = 0; int alphacount = 0; bool inword = false; while ((inchar = fgetc(infile)) != EOF) { count += 1; if (isalpha(inchar)) { alphacount += 1; inword = true; fputc(tolower(inchar), outfile); } else { if (inword) fputc('\n', outfile); inword = false; } } fclose(infile); fclose(outfile); printf("%d alphabetic characters, %d characters in all\n", alphacount, count); return 0; }