22.7 Character-Handling Library

Most data is entered into computers as characters—including letters, digits and various special symbols. In this section, we discuss C++’s capabilities for examining and manipulating individual characters. In the remainder of the chapter, we continue the discussion of character-string manipulation that we began in Chapter 8.

The character-handling library includes several functions that perform useful tests and manipulations of character data. Each function receives a character—represented as an int—or EOF as an argument. Characters are often manipulated as integers. Remember that EOF normally has the value –1 and that some hardware architectures do not allow negative values to be stored in char variables. Therefore, the character-handling functions manipulate characters as integers. Figure 22.17 summarizes the functions of the character-handling library. When using functions from the character-handling library, include the <cctype> header.

Fig. 22.17 Character-handling library functions.

Prototype Description
int isdigit(int c) Returns 1 if c is a digit and 0 otherwise.
int isalpha(int c) Returns 1 if c is a letter and 0 otherwise.
int isalnum(int c) Returns 1 if c is a digit or a letter and 0 otherwise.
int isxdigit(int c) Returns 1 if c is a hexadecimal digit character and 0 otherwise. (See Appendix D for a detailed explanation of binary, octal, decimal and hexadecimal numbers.)
int islower(int c) Returns 1 if c is a lowercase letter and 0 otherwise.
int isupper(int c) Returns 1 if c is an uppercase letter; 0 otherwise.
int tolower(int c) If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged.
int toupper(int c) If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper returns the argument unchanged.
int isspace(int c) Returns 1 if c is a whitespace character—newline (' '), space (''), form feed ('f'), carriage return (' '), horizontal tab (' '), or vertical tab ('v')—and 0 otherwise.
int iscntrl(int c) Returns 1 if c is a control character, such as newline (' '), form feed ('f'), carriage return (' '), horizontal tab (' '), vertical tab ('v'), alert ('a'), or backspace ('')—and 0 otherwise.
int ispunct(int c) Returns 1 if c is a printing character other than a space, a digit, or a letter and 0 otherwise.
int isprint(int c) Returns 1 if c is a printing character including space ('') and 0 otherwise.
int isgraph(int c) Returns 1 if c is a printing character other than space ('') and 0 otherwise.

Figure 22.18 demonstrates functions isdigit, isalpha, isalnum and isxdigit. Function isdigit determines whether its argument is a digit (09). Function isalpha determines whether its argument is an uppercase letter (A-Z) or a lowercase letter (az). Function isalnum determines whether its argument is an uppercase letter, a lowercase letter or a digit. Function isxdigit determines whether its argument is a hexadecimal digit (AF, af, 09).

Fig. 22.18 Character-handling functions isdigit, isalpha, isalnum and isxdigit.

Alternate View

 1  // Fig. 22.18: fig22_18.cpp
 2  // Character-handling functions isdigit, isalpha, isalnum and isxdigit.
 3  #include <iostream>
 4  #include <cctype> // character-handling function prototypes
 5  using namespace std;
 6
 7   int main() {
 8      cout << "According to isdigit:
"
 9         << (isdigit('8') ? "8 is a" : "8 is not a") << " digit
"
10         << (isdigit('#') ? "# is a" : "# is not a") << " digit
";
11
12      cout << "
According to isalpha:
"
13         << (isalpha('A') ? "A is a" : "A is not a") << " letter
"
14         << (isalpha('b') ? "b is a" : "b is not a") << " letter
"
15         << (isalpha('&') ? "& is a" : "& is not a") << " letter
"
16         << (isalpha('4') ? "4 is a" : "4 is not a") << " letter
";
17
18      cout << "
According to isalnum:
"
19         << (isalnum('A') ? "A is a" : "A is not a")
20         << " digit or a letter
"
21         << (isalnum('8') ? "8 is a" : "8 is not a")
22         << " digit or a letter
"
23         << (isalnum('#') ? "# is a" : "# is not a")
24         << " digit or a letter
";
25
26     cout << "
According to isxdigit:
"
27        << (isxdigit('F') ? "F is a" : "F is not a")
28        << " hexadecimal digit
"
29        << (isxdigit('J') ? "J is a" : "J is not a")
30        << " hexadecimal digit
"
31        << (isxdigit('7') ? "7 is a" : "7 is not a")
32        << " hexadecimal digit
"
33        << (isxdigit('$') ? "$ is a" : "$ is not a")
34        << " hexadecimal digit
"
35        << (isxdigit('f') ? "f is a" : "f is not a")
36        << " hexadecimal digit" << endl;
37  }

According to isdigit:
8 is a digit
# is not a digi

According to isalpha:
A is a letter
b is a letter
& is not a letter
4 is not a letter

According to isalnum:
A is a digit or a letter
8 is a digit or a letter
# is not a digit or a letter

According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit
$ is not a hexadecimal digit
f is a hexadecimal digit

Figure 22.18 uses the conditional operator (?:) with each function to determine whether the string " is a " or the string " is not a " should be printed in the output for each character tested. For example, line 9 indicates that if '8' is a digit—i.e., if isdigit returns a true (nonzero) value—the string "8 is a " is printed. If '8' is not a digit (i.e., if isdigit returns 0), the string "8 is not a " is printed.

Figure 22.19 demonstrates functions islower, isupper, tolower and toupper. Function islower determines whether its argument is a lowercase letter (a–z). Function isupper determines whether its argument is an uppercase letter (A–Z). Function tolower converts an uppercase letter to lowercase and returns the lowercase letter—if the argument is not an uppercase letter, tolower returns the argument value unchanged. Function toupper converts a lowercase letter to uppercase and returns the uppercase letter—if the argument is not a lowercase letter, toupper returns the argument value unchanged.

Fig. 22.19 Character-handling functions islower, isupper, tolower and toupper.

Alternate View

 1  // Fig. 22.19: fig22_19.cpp
 2  // Character-handling functions islower, isupper, tolower and toupper.
 3  #include <iostream>
 4  #include <cctype> // character-handling function prototypes
 5  using namespace std;
 6
 7  int main() {
 8     cout << "According to islower:
"
 9        << (islower('p') ? "p is a" : "p is not a")
10        << " lowercase letter
"
11        << (islower('P') ? "P is a" : "P is not a")
12        << " lowercase letter
"
13        << (islower('5') ? "5 is a" : "5 is not a")
14        << " lowercase letter
"
15        << (islower('!') ? "! is a" : "! is not a")
16        << " lowercase letter
";
17
18     cout << "
According to isupper:
"
19        << (isupper('D') ? "D is an" : "D is not an")
20        << " uppercase letter
"
21        << (isupper('d') ? "d is an" : "d is not an")
22        << " uppercase letter
"
23        << (isupper('8') ? "8 is an" : "8 is not an")
24        << " uppercase letter
"
25        << (isupper('$') ? "$ is an" : "$ is not an")
26        << " uppercase letter
";
27
28     cout << "
u converted to uppercase is "
29        << static_cast<char>(toupper('u'))
30        << "
7 converted to uppercase is "
31        << static_cast<char>(toupper('7'))
32        << "
$ converted to uppercase is "
33        << static_cast<char>(toupper('$'))
34        << "
L converted to lowercase is "
35        << static_cast<char>(tolower('L')) << endl;
36  }
According to islower:
p is a lowercase letter
P is not a lowercase letter
5 is not a lowercase letter
! is not a lowercase letter

According to isupper:
D is an uppercase letter
d is not an uppercase letter
8 is not an uppercase letter
$ is not an uppercase letter

u converted to uppercase is U
7 converted to uppercase is 7
$ converted to uppercase is $
L converted to lowercase is l

Figure 22.20 demonstrates functions isspace, iscntrl, ispunct, isprint and isgraph. Function isspace determines whether its argument is a whitespace character, such as space (' '), form feed ('f'), newline (' '), carriage return (' '), horizontal tab (' ') or vertical tab ('v'). Function iscntrl determines whether its argument is a control character such as horizontal tab (' '), vertical tab ('v'), form feed ('f'), alert ('a'), backspace (''), carriage return (' ') or newline (' '). Function ispunct determines whether its argument is a printing character other than a space, digit or letter, such as $, #, (,), [,], {, }, ;, : or %. Function isprint determines whether its argument is a character that can be displayed on the screen (including the space character). Function isgraph tests for the same characters as isprint, but the space character is not included.

Fig. 22.20 Character-handling functions isspace, iscntrl, ispunct, isprint and isgraph

Alternate View

 1  // Fig. 22.20: fig22_20.cpp
 2  // Using functions isspace, iscntrl, ispunct, isprint and isgraph.
 3  #include <iostream>
 4  #include <cctype> // character-handling function prototypes
 5  using namespace std;
 6
 7  int main() {
 8     cout << "According to isspace:
Newline "
 9        << (isspace('
') ? "is a" : "is not a")
10        << " whitespace character
Horizontal tab "
11        << (isspace('	') ? "is a" : "is not a")
12        << " whitespace character
"
13        << (isspace('%') ? "% is a" : "% is not a")
14        << " whitespace character
";
15
16     cout << "
According to iscntrl:
Newline "
17        << (iscntrl('
') ? "is a" : "is not a")
18        << " control character
"
19        << (iscntrl('$') ? "$ is a" : "$ is not a")
20        << " control character
";
21
22     cout << "
According to ispunct:
"
23        << (ispunct(';') ? "; is a" : "; is not a")
24        << " punctuation character
"
25        << (ispunct('Y') ? "Y is a" : "Y is not a")
26        << " punctuation character
"
27        << (ispunct('#') ? "# is a" : "# is not a")
28        << " punctuation character
";
29
30     cout << "
According to isprint:
"
31        << (isprint('$') ? "$ is a" : "$ is not a")
32        << " printing character
Alert "
33        << (isprint('a') ? "is a" : "is not a")
34        << " printing character
Space "
35        << (isprint(' ') ? "is a" : "is not a")
36        << " printing character
";
37
38     cout << "
According to isgraph:
"
39        << (isgraph('Q') ? "Q is a" : "Q is not a")
40        << " printing character other than a space
Space "
41        << (isgraph(' ') ? "is a" : "is not a")
42        << " printing character other than a space" << endl;
43  }

According to isspace:
Newline is a whitespace character
Horizontal tab is a whitespace character
% is not a whitespace character

According to iscntrl:
Newline is a control character
$ is not a control character

According to ispunct:
; is a punctuation character
Y is not a punctuation character
# is a punctuation character

According to isprint:
$ is a printing character
Alert is not a printing character
Space is a printing character

According to isgraph:
Q is a printing character other than a space
Space is not a printing character other than a space
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset