ctype Overview ctype
#include <ctype.h>
The ctype macros and functions test a character's type, and can
transform some characters into others. They are as follows:
isalnum()Test if alphanumeric character
isalpha()Test if alphabetic character
isascii()Test if ASCII character
iscntrl()Test if a control character
isdigit()Test if a numeric digit
islower()Test if lower-case character
isprint()Test if printable character
ispunct()Test if punctuation mark
isspace()Test if a tab, space, or return
isupper()Test if upper-case character
_tolower()Change to lower-case character
_toupper()Change to upper-case character
These are defined in the header file ctype.h, and each is
described further in its own Lexicon entry.
***** Example *****
The following example demonstrates the macros isalnum, isalpha,
isascii, iscntrl, isdigit, islower, isprint, ispunct, and
isspace. It prints information about the type of characters it
contains.
#include <ctype.h>
#include <stdio.h>
main()
{
FILE *fp;
char fname[20];
int ch;
int alnum = 0;
int alpha = 0;
int allow = 0;
int control = 0;
int printable = 0;
int punctuation = 0;
int space = 0;
printf("Enter name of text file to examine: ");
fflush(stdout);
gets(fname);
COHERENT Lexicon Page 1
ctype Overview ctype
if ((fp = fopen(fname, "r")) != NULL) {
while ((ch = fgetc(fp)) != EOF) {
if (isascii(ch)) {
if (isalnum(ch))
alnum++;
if (isalpha(ch))
alpha++;
if (islower(ch))
allow++;
if (iscntrl(ch))
control++;
if (isprint(ch))
printable++;
if (ispunct(ch))
punctuation++;
if (isspace(ch))
space++;
} else {
printf("%s is not ASCII.\n",
fname);
exit(1);
}
}
printf("%s has the following:\n", fname);
printf("%d alphanumeric characters\n", alnum);
printf("%d alphabetic characters\n", alpha);
printf("%d alphabetic lower-case characters\n",
allow);
printf("%d control characters\n", control);
printf("%d printable characters\n", printable);
printf("%d punctuation marks\n", punctuation);
printf("%d white space characters\n", space);
exit(0);
} else {
printf("Cannot open \"%s\".\n", fname);
exit(2);
}
}
COHERENT Lexicon Page 2
ctype Overview ctype
***** See Also *****
ctype.h, libraries
COHERENT Lexicon Page 3