

ctype                        Overview                       ctype



#include <ctype.h>

The ctype  macros and functions test a  character's _t_y_p_e, and can
transform some characters into others.  They are as follows:

    iissaallnnuumm()Test if alphanumeric character
    iissaallpphhaa()Test if alphabetic character
    iissaasscciiii()Test if ASCII character
    iissccnnttrrll()Test if a control character
    iissddiiggiitt()Test if a numeric digit
    iisslloowweerr()Test if lower-case character
    iisspprriinntt()Test if printable character
    iissppuunncctt()Test if punctuation mark
    iissssppaaccee()Test if a tab, space, or return
    iissuuppppeerr()Test if upper-case character
    _ttoolloowweerr()Change to lower-case character
    _ttoouuppppeerr()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


