Chapter 15 - Power Programming: Tapping Important C and C++ Libraries
Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
Copyright 1998 The McGraw-Hill Companies
The Character Functions (CTYPE.H)
Characters are defined in most languages as single-byte values. Chinese is one case where 2 bytes are needed. The character macros and functions in C and C++, prototyped or contained in CTYPE.H, take integer arguments but utilize only the lower byte of the integer value. Automatic type conversion usually permits character arguments to also be passed to the macros or functions. The macros and functions shown in Table 15-4 are available.
Table 15-4: Character Macros Available in C and C++
Macro
Description
isalnum( )
Checks for alphanumeric character
isalpha( )
Checks for alpha character
isascii( )
Checks for ASCII character
iscntrl( )
Checks for control character
isdigit( )
Checks for decimal digit (0–9)
isgraph( )
Checks for printable character (no space)
islower( )
Checks for lowercase character
isprint( )
Checks for printable character
ispunct( )
Checks for punctuation character
isspace( )
Checks for white-space character
isupper( )
Checks for uppercase character
isxdigit( )
Checks for hexadecimal digit
toascii( )
Translates character to ASCII equivalent
tolower( )
Translates character to lowercase
toupper( )
Translates character to uppercase
These macros and functions allow characters to be tested for various conditions or to be converted between lowercase and uppercase.
Checking for Alphanumeric, Alpha, and ASCII Values
The macros shown in Table 15-5 allow ASCII-coded integer values to be checked with the use of a lookup table.
Table 15-5: Three Important Macros
Macro
Description
int isalnum(ch)
Checks for alphanumeric values A–Z, a–z, and 0–9. ch0 is an integer
int isalpha(ch)
Checks for alpha values A–Z and a–z. ch0 is an integer
int isascii(ch)
Checks for ASCII values 0–127 (0–7Fh). ch is an integer
The following program checks the ASCII integer values from zero to 127 and reports which of the preceding three functions produce a TRUE condition for each case:
/* * alpha.c * Demonstrating the use of the isalnum( ), isalpha( ), * and isascii( ) library functions. * Copyright (c) Chris H. Pappas and William H. Murray, 1998 */
A portion of the information sent to the screen is shown here:
The ASCII digit 0 is an: ascii char
The ASCII digit 1 is an: ascii char . . . The ASCII digit 48 is an: alpha-numeric char ascii char
The ASCII digit 49 is an: alpha-numeric char ascii char . . . The ASCII digit 65 is an: alpha-numeric char alpha char ascii char
The ASCII digit 66 is an: alpha-numeric char alpha char ascii char
These functions are very useful in checking the contents of string characters.
Checking for Control, White Space, and Punctuation
The routines shown in Table 15-6 are implemented as both macros and functions.
Table 15-6: Routines Implemented as Both Macros and Functions
Routine
Description
int iscntrl(ch)
Checks for control character
int isdigit(ch)
Checks for digits 0–9
int isgraph(ch)
Checks for printable characters (no space)
int islower(ch)
Checks for lowercase a–z
int isprint(ch)
Checks for printable character
int ispunct(ch)
Checks for punctuation
int isspace(ch)
Checks for white space
int isupper(ch)
Checks for uppercase A–Z
int isxdigit(ch)
Checks for hexadecimal value 0–9, a–f, or A–F
These routines allow ASCII-coded integer values to be checked via a lookup table. A zero is returned for FALSE and a nonzero for TRUE. A valid ASCII character set is assumed. The value ch is an integer.
The next application checks the ASCII integer values from zero to 127 and reports which of the preceding nine functions give a TRUE condition for each value:
/* * contrl.c * Demonstrating several character functions such as * isprint( ), isupper( ), iscntrl( ), etc. * Copyright (c) Chris H. Pappas and William H. Murray, 1998 */
A portion of the information sent to the screen is shown here:
The ASCII digit 0 is a(n): control char
The ASCII digit 1 is a(n): control char . . . The ASCII digit 32 is a(n): printable char space char
The ASCII digit 33 is a(n): printable char punctuation char graphics char
The ASCII digit 34 is a(n): printable char punctuation char graphics char . . . The ASCII digit 65 is a(n): printable char uppercase char graphics char hexadecimal char
The ASCII digit 66 is a(n): printable char uppercase char graphics char hexadecimal char
Conversions to ASCII, Lowercase, and Uppercase
The macros and functions shown in Table 15-7 allow ASCII-coded integer values to be translated.
Table 15-7: Functions Used to Translate ASCll-Coded Integer Values
Macro
Description
int toascii(ch)
Translates to ASCII character
int tolower(ch)
Translates ch to lowercase if uppercase
int _tolower(ch)
Translates ch to lowercase
int toupper(ch)
Translates ch to uppercase if lowercase
int _toupper(ch)
Translates ch to uppercase
The macro toascii( ) converts ch to ASCII by retaining only the lower 7 bits. The functions tolower( ) and toupper( ) convert the character value to the format specified. The macros _tolower( ) and _toupper( ) return identical results when supplied proper ASCII values. A valid ASCII character set is assumed. The value ch is an integer.
The next example shows how the macro toascii( ) converts integer information to correct ASCII values:
/* * ascii.c * Demonstrating the use of the toascii( ) function. * Copyright (c) Chris H. Pappas and William H. Murray, 1998 */
#include <stdio.h> #include <ctype.h>
int ch;
main( ) { for(ch=0;ch<=512;ch++) { printf(“The ASCII value for %d is %d\n”, ch,toascii(ch)); } return (0); }
Here is a partial list of the information sent to the screen:
The ASCII value for 0 is 0 The ASCII value for 1 is 1 The ASCII value for 2 is 2 . . . The ASCII value for 128 is 0 The ASCII value for 129 is 1 The ASCII value for 130 is 2 . . . The ASCII value for 256 is 0 The ASCII value for 257 is 1 The ASCII value for 258 is 2 . . . The ASCII value for 384 is 0 The ASCII value for 385 is 1 The ASCII value for 386 is 2