Exercise 5 page 330

Solution index                   Textual C program


/* Count both uppercase and lowercase letters separately. */

#include <stdio.h>
#include <ctype.h>

int main(void)
{
   int   c, i, uc_letter[26], lc_letter[26];

   for (i = 0; i < 26; ++i){        /* init array to zero*/
     uc_letter[i] = 0; lc_letter[i] = 0; 
   }

   while ((c = getchar()) != EOF)  /* count the letters */
      if (isupper(c))
         ++uc_letter[c - 'A'];
      else if (islower(c))
         ++lc_letter[c - 'a'];

   for (i = 0; i < 26; ++i) {      /* print the results */
      if (i % 3 == 0)
         printf("\n");
      printf("%4c:%3d", 'A' + i, uc_letter[i]);
      printf("%4c:%3d", 'a' + i, lc_letter[i]);
   }

   printf("\n\n");
   return 0;
}

The starting point of this exercise is the program in section 9.2, page 307 - 309 i C by Dissection. Here we extend the program to also count lowercase letters. You should first read the dissection of the original program in the book, and then read the dissections of the extensions below.
 

   int   c, i, uc_letter[26], lc_letter[26];
We go for a solution where we keep track of lowercase and uppercase letters in two separate arrays. We therefore declare both an uppercase array uc_letters and a lowercase array lc_letter.
 

   for (i = 0; i < 26; ++i){        /* init array to zero*/
     uc_letter[i] = 0; lc_letter[i] = 0; 
   }
Both uc_letter and lc_letter are initiaized in the same for loop
 

   while ((c = getchar()) != EOF)  /* count the letters */
      if (isupper(c))
         ++uc_letter[c - 'A'];
      else if (islower(c))
         ++lc_letter[c - 'a'];
We extend the if control structure in the while loop with an 'else if', which counts lower case letters.
 

   for (i = 0; i < 26; ++i) {      /* print the results */
      if (i % 3 == 0)
         printf("\n");
      printf("%4c:%3d", 'A' + i, uc_letter[i]);
      printf("%4c:%3d", 'a' + i, lc_letter[i]);
   }
In the for loop that prints the final counts, we print uppercase and lowercase letters side by side. We compensate for this 'double letter printing' by printing newlines when i is 3, 6, 9, etc. With this, we keep the six comlumn output layout.
 

We can, for instance, execute the program with the source program as input. Hereby we are able to count the number of individual letters in the program. I represent the source program in the file count-us-lc.c. By running the command a.out a.out < count-uc-lc.c we get the following output:

   A:  2   a: 11   B:  0   b:  1   C:  1   c: 23
   D:  0   d:  7   E:  1   e: 40   F:  1   f:  9
   G:  0   g:  1   H:  0   h:  7   I:  0   i: 34
   J:  0   j:  0   K:  0   k:  0   L:  0   l: 22
   M:  0   m:  1   N:  0   n: 18   O:  1   o: 11
   P:  0   p: 11   Q:  0   q:  0   R:  0   r: 29
   S:  0   s: 11   T:  0   t: 40   U:  0   u: 12
   V:  0   v:  1   W:  0   w:  3   X:  0   x:  0
   Y:  0   y:  3   Z:  0   z:  1



 


Generated: Wednesday, March 29, 2006, 12:33:32
This program dissection page is generated from an XML-in-LAML source file