Thursday, June 16, 2011

Character Class Tests

5.  <ctype.h>  Character Class Tests

The header <ctype.h> declares functions for testing characters. For each function, the argument list is an int, whose value must be EOF or representable as an unsigned char, and the return value is an int. The functions return non-zero (true) if the argument c satisfies the condition described, and zero if not.

isupper(c)
upper-case letter
islower(c)
lower-case letter
isalpha(c)
isupper(c) or islower(c) is true
isdigit(c)
decimal digit
isxdigit(c)
hexadecimal digit
isalnum(c)
isalpha(c) or isdigit(c) is true
iscntrl(c)
control character
isprint(c)
printing character including space
isgraph(c)
printing character except space
ispunct(c)
printing character except space or letter or digit
isspace(c)
space, formfeed, newline, carriage return, tab, vertical tab

In the seven-bit ASCII character set, the control characters are 0 (NULL) to 0x1F (US, Ctrl-_), and 0x7F (DEL); the printing characters are 0x20 (space, ' ') to 0x7E ('-').
In addition, there are two functions that convert the case of letters:

int toupper(c)
convert c to upper case
int tolower(c)
convert c to lower case

If c is an upper-case letter, tolower(c) returns the corresponding lower-case letter, toupper(c) returns the corresponding upper-case letter; otherwise it returns c.

EXAMPLE CODES

1.       ctype.h
l ctype.c
#include <stdio.h>
#include <ctype.h>
void main() {
    char s[] = "한글(Korean Alphabet)", *p;
    for (p = s; *p; p++) *p = toupper(*p);
    puts(s);    // 한글(KOREAN ALPHABET)

    printf("isalpha('%c'): %s\n", '_', isalpha('_') ? "TRUE" : "FALSE");
    // isalpha('_'): FALSE
    printf("ispunct('%c'): %s\n", '_', ispunct('_') ? "TRUE" : "FALSE");
    // ispunct('_'): TRUE
}

No comments: