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.

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

[Shaka@localhost c-lang]$ cc arrays.c 
[Shaka@localhost c-lang]$ ./a.out 
hello world 123321 010459
haha~~~
digits = 2 3 2 2 1 1 0 0 0 1, white space = 5, other = 17
[Shaka@localhost c-lang]$ 

The declaration

int ndigit[10];

declares ndigit to be an array of 10 integers. Array subscripts always start at zero in C, so the elements are ndigit[0], ndigit[1], ..., ndigit[9]. This is reflected in the for loops that initialize and print the array.

A subscript can be any integer expression, which includes integer variables like i, and integer constants.

This particular program relies on the properties of the character representation of the digits. For example, the test

if (c >= '0' && c <= '9')

determines whether the character in c is a digit. If it is, the numeric value of that digit is

c-'0'

This works only if '0', '1', ..., '9' have consecutive increasing values. Fortunately, this is true for all character sets.

By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. This is natural and convenient; for example c-'0' is an integer expression with a value between 0 and 9 corresponding to the character '0' to '9' stored in c, and thus a valid subscript for the array ndigit.

The decision as to whether a character is a digit, white space, or something else is made with the sequence

if (c >= '0' && c <= '9')
	++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
	++nwhite;
else
	++nother;

The pattern

if (condition1)
	statement1
else if (condition2)
	statement2
	...
	...
else
	statementn

occurs frequently in programs as a way to express a multi-way decision. The conditions are evaluated in order from the top until some condition is satisfied; at that point the corresponding statement part is executed, and the entire construction is finished. (Any statement can be several statements enclosed in braces.) If none of the conditions is satisfied, the statement after the final else is executed if it is present. If the final else and statement are omitted, as in the word count program on the last chapter, no action takes place. There can be any number of

else if(condition)
	statement

groups between the initial if and the final else.

As a matter of style, it is advisable to format this construction as we have shown; if each if were indented past the previous else, a long sequence of decisions would march off the right side of the page.

The switch statement, to be discussed in the later chapter, provides another way to write a multi-way branch that is particulary suitable when the condition is whether some integer or character expression matches one of a set of constants. For contrast, we will present a switch version of this program in the later chapter.

Next And Prev

Next: Use Functions In C Program

Prev: Character Input and Output

Relate article

The for statement and the symbolic constants

Getting Started With C Language

Character Input and Output

Variables and Arithmetic Expressions

Use Functions In C Program

Character Arrays