maandag 18 juni 2007

The CCTYPE Library of Character functions

C++ has inherited from C a handy package of character-related functions, prototyped in the cctype header file. That simplify such tasks as determining whether a character is an uppercase letter or a digit or punctuation.
For example isalpha(ch) function returns a nonzero value if ch is a letter. and zero otherwise.
Similarly, the ispunct(ch) function returns a true value only if ch is a punctuation character, such as a comma or period.
(These functions have a return type int rather then bool, but the usual bool conversions allow you to treat them as type bool.)

Using these functions is more convenient than using the AND or OR operators.
For example, here's how you might use AND and OR to test whether a character ch is an alphabetic character:
if ((ch >= 'a' && ch <= 'z') (ch >= 'A' && ch <= 'Z'))
Compare it to using isalpha():
if (isalpha(ch))

Not only is isalpha() easier to use, it is more general. The AND/OR form assumes that character codes for A through Z are in sequence, with no other characters having codes in that range. This assumption is true for ASCII codes, but isn't always true in general!

The cctype Character functions

isalnum() This function returns true if the argument is alphanumeric, that is a letter or a digit.

isalpha() This function returns true if the argument is alphabetic

isblank() This function returns true if argument is a space or horizontal tab

iscntrl() This function returns true if the argument is a control character

isdigit() This function returns true if the argument is a decimal digit (0-9)

isgraph() This function returns true if the argument is any printing character other then space

islower() This function returns true if the argument is a lowercase letter

isprint() This function returns true if the argument is any printing character including a space

ispunct() This function returns true if the argument is a punctuation character

isspace() This function returns true if the argument is a standard whitespace character (that is space, formfeed, newline, carriage return, horizontal tab, vertical tab).

isupper() This function returns true if the argument is a uppercase letter.

isxdigit() This function returns true if the argument is a hexadecimal digit character ( that is 0-9, a-f, or A-F).

tolower() If the argument is an uppercase character, tolower() returns the lowercase version of that character; otherwise it returns the argument unaltered.

toupper() If the argument is a lowercase character, toupper() returns the uppercase version of that character; otherwise, it returns the argument unaltered.

Geen opmerkingen: