/* letters.c */ /* This program performs a case-insensitive analysis of frequency of occurrence in a * text file. The analysis ignores all non-alphabetic characters. * * Input is from stdin; Output is to stdout; Error is to stderr. * * One optional command line parameter allows specification of the floor for * printing out double and triple letter occurrences. Occurrence statistics less * than the floor will not be displayed except for single letters of which all 26 * are printed. The default floor (used when none is specified) is given by DEFFL. */ #include #define LET 26 #define DEFFL 0.005 /* Default floor of 0.5% */ /* Letter occurrence counters */ static int singles[LET] = {0}; static int doubles[LET][LET] = {0}; static int triples[LET][LET][LET] = {0}; int main(int argc, char **argv) { double lowperc = DEFFL; /* floor for printing doubles and triples */ int prev, last, next; /* characters read */ int i, j, k; /* loop counters */ int letcount; /* number of letters */ double perc; /* intermediate temporary storage of percentage */ if( argc == 2 ) sscanf(argv[1], "%lf", &lowperc); else if( argc > 2 ) { fprintf(stderr, "\nUsage: %s [floor]\n", argv[0]); return 1; } /* end if */ /* Process first letter */ firstchar: if( (prev = tolower(getchar())) == EOF ) { fprintf(stderr, "\nThe file contains no letters."); return 1; } else if( (prev >= (int)'a') && (prev <= (int)'z') ) ++singles[prev -= (int)'a']; else goto firstchar; /* Process second letter */ secondchar: if( (last = tolower(getchar())) == EOF ) { fprintf(stderr, "\nThe file contains only one letter: %c", (char)(prev + (int)'a')); return 1; } else if( (last >= (int)'a') && (last <= (int)'z') ) { ++doubles[prev][last -= (int)'a']; ++singles[last]; } else goto secondchar; /* Process rest of input */ letcount = 2; while( (next = tolower(getchar())) != EOF ) { if( (next < (int)'a') || (next > (int)'z') ) continue; /* process this character */ ++singles[next -= (int)'a']; ++doubles[last][next]; ++triples[prev][last][next]; prev = last; last = next; ++letcount; } /* end while */ /* Print stats */ printf("\nThe file contains %d letters.", letcount); printf("\n\nSINGLE LETTERS:\n"); printf("----------------------------------------------------------------------------"); for( i = 0; i < LET; i++ ) printf("\n%c: %lf", (char)(i + (int)'a'), (double)singles[i]/letcount); printf("\n\nDOUBLE LETTERS:\n"); printf("----------------------------------------------------------------------------"); for( i = 0; i < LET; i++ ) for( j = 0; j < LET; j++ ) if( (perc = (double)doubles[i][j]/(letcount-1)) >= lowperc ) printf("\n%c%c: %lf", (char)(i + (int)'a'), (char)(j + (int)'a'), perc); printf("\n\nTRIPLE LETTERS:\n"); printf("----------------------------------------------------------------------------"); for( i = 0; i < LET; i++ ) for( j = 0; j < LET; j++ ) for( k = 0; k < LET; k++ ) if( (perc = (double)triples[i][j][k]/(letcount-2)) >= lowperc ) printf("\n%c%c%c: %lf", (char)(i + (int)'a'), (char)(j + (int)'a'), (char)(k + (int)'a'), perc); return 0; }