Arrays: Let is write a program to count the number of occurrences of each digit, of white space
characters (blank, tab, newline), and of all other characters. This is artificial, but it permits us
to illustrate several aspects of C in one program.
There are twelve categories of input, so it is convenient to use an array to hold the number of
occurrences of each digit, rather than ten individual variables. Here is one version of the
program:
#include<stdio.h>
/* count digits, white space, others */
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n",
nwhite, nother);
}
The output of this program on itself is
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
No comments:
Post a Comment