Classifying characters from a string
suggest change#include <ctype.h>
#include <stddef.h>
typedef struct {
size_t space;
size_t alnum;
size_t punct;
} chartypes;
chartypes classify(const char *s) {
chartypes types = { 0, 0, 0 };
const char *p;
for (p= s; p != '\0'; p++) {
types.space += !!isspace((unsigned char)*p);
types.alnum += !!isalnum((unsigned char)*p);
types.punct += !!ispunct((unsigned char)*p);
}
return types;
}
The classify function examines all characters from a string and counts the number of spaces, alphanumeric and punctuation characters. It avoids several pitfalls.
- The character classification functions (e.g.
isspace) expect their argument to be either representable as anunsigned char, or the value of theEOFmacro. - The expression
*pis of typecharand must therefore be converted to match the above wording. - The
chartype is defined to be equivalent to eithersigned charorunsigned char. - When
charis equivalent tounsigned char, there is no problem, since every possible value of thechartype is representable asunsigned char. - When
charis equivalent tosigned char, it must be converted tounsigned charbefore being passed to the character classification functions. And although the value of the character may change because of this conversion, this is exactly what these functions expect. - The return value of the character classification functions only distinguishes between zero (meaning
false) and nonzero (meaningtrue). For counting the number of occurrences, this value needs to be converted to a 1 or 0, which is done by the double negation,!!.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents