#include <ctype.h>
#include <stdio.h>
typedef struct {
size_t space;
size_t alnum;
size_t punct;
} chartypes;
chartypes classify(FILE *f) {
chartypes types = { 0, 0, 0 };
int ch;
while ((ch = fgetc(f)) != EOF) {
types.space += !!isspace(ch);
types.alnum += !!isalnum(ch);
types.punct += !!ispunct(ch);
}
return types;
}
The classify
function reads characters from a stream and counts the number of spaces, alphanumeric and punctuation characters. It avoids several pitfalls.
int
, since otherwise there would be an ambiguity between reading EOF
(the end-of-file marker) and a character that has the same bit pattern.isspace
) expect their argument to be either representable as an unsigned char
, or the value of the EOF
macro. Since this is exactly what the fgetc
returns, there is no need for conversion here.false
) and nonzero (meaning true
). For counting the number of occurrences, this value needs to be converted to a 1 or 0, which is done by the double negation, !!
.