/* count_char - count the number of alphabetic characters in an input file */ /* and print a text histogram of the counts. */ /* */ /* After compiling, use the program to count the characters of a text file */ /* as follows. Suppose you want to count the characters in proj1.c: */ /* */ /* ./a.out < proj1.c */ #include #define ALPHALEN 26 void histogram( int[], int, char[] ); int main() { /* (1) Create an integer array named 'letters' of length ALPHALEN. */ /* 'letters' will be used to hold the counts of each letter. */ /* (2) Create a character array named 'labels' of length ALPHALEN. */ /* 'labels' will hold character labels for the histogram. */ int numletters; /* counts number of letters in input */ char c; int i; /* Initialize numletters, letters, and labels */ numletters = 0; /* (3) Use a for loop to initialize the elements of 'letters' to zero. */ /* You should use the ALPHALEN constant in the test part of the loop. */ /* (4) User a for loop to initialize the elements of 'labels' to the */ /* characters 'A', 'B', ..., 'Z'. */ /* Loop over characters in input and count occurrences of each letter */ /* Note: counts for upper and lower case are merged */ while ( (c = getchar()) != EOF ) { numletters++; if ( c >= 'a' && c <= 'z' ) { /* if c is lowercase a..z */ letters[ c - 'a']++; } else if (c >= 'A' && c <= 'Z') { /* if c is uppercase A..Z */ /* (5) The two lines before this else-if block test whether c is a */ /* lowercase letter and, if so, increments the appropriate count. */ /* This block tests whether c is an uppercase letter. Write a */ /* statement to incrment the correct element of the 'letters' */ /* array. Hint: it is almost the same as the line */ /* letters[ c - 'a']++; */ } else { /* otherwise c is not a letter */ numletters--; } } /* Generate the histogram */ histogram(letters, ALPHALEN, labels); return 0; } /* histogram() - produce a text histogram of the data in counts[] using */ /* labels from the labels[] array. Both counts[] and labels[] must */ /* have length len. */ void histogram(int counts[], int len, char labels[]) { int i, j; for (i=0; i