sample3.c

/*
 * counts digits, white space, others from the standard input
 *
 * this program has bugs -- comments omitted to hide it
 * used to demonstrate static and dynamic debugging
 *
 * Matt Bishop, ECS 36A, Fall 2019
 */
#include <stdio.h>

int main(void)
{
	int c, i, num_digits[10], num_white, num_other;
	num_white = num_other = 0;
	for(i = 0; i < 10; i++)
		num_digits[i] = 0;
	while(c = getchar() != EOF){
		switch(c){
		case '0': case '1': case '2': case '3':
		case '4': case '5': case '6': case '7':
		case '8': case '9':
			num_digits[c - '0']++;
			break;
		case ' ': case '\t': case '\n':
			num_white++;
			break;
		default:
			num_other++;
			break;
		}
	}
	printf("digits =");
	for(i = 0; i <= 10; i++)
		printf(" %d", num_digits[i]);
	printf("white space = %d, other = %d\n",
		num_white, num_other);

	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh