Contents    Page-10    Prev    Next    Page+10    Index   

Implementation of Character Classes

Character class names are defined as small-integer constants. A character class array is initialized to map from a character code to the appropriate class.


#define ALPHA   1      /* char class names */  
#define NUMERIC 2
#define SPECIAL 3

int CHARCLASS[256];    /* char class array */

char specchar[] = "+-*/:=<>^.,;()[]{}";

    for (i = 'a'; i <= 'z'; ++i)   /* init */
       CHARCLASS[i] = ALPHA;
    for (i = '0'; i <= '9'; ++i)
       CHARCLASS[i] = NUMERIC;
    for (i = 0 ; specchar[i] != '\0'; ++i)
       CHARCLASS[specchar[i]] = SPECIAL;
The class of a character is looked up in the array:

    c = peekchar();
    if (CHARCLASS[c] == ALPHA) ...