www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 1078 (continued)]

22.9. Character-Handling Library

Most data is entered into computers as charactersincluding 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 characterrepresented as an intor 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 file.

Figure 22.17. Character-handling library functions.

[Page 1079]

Prototype

Description

int isdigit( int c )

Returns true if c is a digit and false otherwise.

int isalpha( int c )

Returns true if c is a letter and false otherwise.

int isalnum( int c )

Returns true if c is a digit or a letter and false otherwise.

int isxdigit( int c )

Returns TRue if c is a hexadecimal digit character and false otherwise. (See Appendix D, Number Systems, for a detailed explanation of binary, octal, decimal and hexadecimal numbers.)

int islower( int c )

Returns true if c is a lowercase letter and false otherwise.

int isupper( int c )

Returns TRue if c is an uppercase letter; false 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 true if c is a white-space characternewline ('\n'), space (' '), form feed ('\f'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v')and false otherwise.

int iscntrl( int c )

Returns TRue if c is a control character, such as newline ('\n'), form feed ('\f'), carriage return ('\r'), horizontal tab ('\t'), vertical tab ('\v'), alert ('\a'), or backspace ('\b')and false otherwise.

int ispunct( int c )

Returns true if c is a printing character other than a space, a digit, or a letter and false otherwise.

int isprint( int c )

Returns true value if c is a printing character including space (' ') and false otherwise.

int isgraph( int c )

Returns TRue if c is a printing character other than space (' ') and false otherwise.



[Page 1079]

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).

Figure 22.18. Character-handling functions isdigit, isalpha, isalnum and isxdigit.
(This item is displayed on pages 1079 - 1080 in the print version)

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

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

  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



[Page 1080]

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 16 indicates that if '8' is a digiti.e., if isdigit returns a true (nonzero) valuethe 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 (az). Function isupper determines whether its argument is an uppercase letter (AZ). Function tolower converts an uppercase letter to a lowercase letter and returns the lowercase letterif the argument is not an uppercase letter, tolower returns the argument value unchanged. Function toupper converts a lowercase letter to an uppercase letter and returns the uppercase letterif the argument is not a lowercase letter, toupper returns the argument value unchanged.


[Page 1081]
Figure 22.19. Character-handling functions islower, isupper, tolower and toupper.
(This item is displayed on pages 1081 - 1082 in the print version)

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

  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



[Page 1082]

Figure 22.20 demonstrates functions isspace, iscntrl, ispunct, isprint and isgraph. Function isspace determines whether its argument is a white-space character, such as space (' '), form feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t') or vertical tab ('\v'). Function iscntrl determines whether its argument is a control character such as horizontal tab ('\t'), vertical tab ('\v'), form feed ('\f'), alert ('\a'), backspace ('\b'), carriage return ('\r') or newline ('\n'). 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; however, the space character is not included.


[Page 1084]
Figure 22.20. Character-handling functions isspace, iscntrl, ispunct, isprint and isgraph.
(This item is displayed on pages 1082 - 1083 in the print version)

 1  // Fig. 22.20: fig22_20.cpp
 2  // Using functions isspace, iscntrl, ispunct, isprint, isgraph.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6
 7  #include <cctype> // character-handling function prototypes
 8  using std::iscntrl;
 9  using std::isgraph;
10  using std::isprint;
11  using std::ispunct;
12  using std::isspace;
13
14  int main()
15  {
16     cout << "According to isspace:\nNewline "
17        << ( isspace( '\n' ) ? "is a" : "is not a" )
18        << " whitespace character\nHorizontal tab "
19        << ( isspace( '\t' ) ? "is a" : "is not a" )
20        << " whitespace character\n"
21        << ( isspace( '%' ) ? "% is a"  : "% is not a" )
22        << " whitespace character\n";
23
24     cout << "\nAccording to iscntrl:\nNewline "
25        << ( iscntr1( '\n' ) ? "is a" : "is not a" )
26        << " control character\n"
27        << ( iscntr1( '$' ) ? "$ is a" : "$ is not a" )
28        << " control character\n";
29
30     cout << "\nAccording to ispunct:\n"
31        << ( ispunct( ';' ) ? "; is a" : "; is not a" )
32        << " punctuation character\n"
33        << ( ispunct( 'Y' ) ? "Y is a" : "Y is not a" )
34        << " punctuation character\n"
35        << ( ispunct( '#' ) ? "# is a" : "# is not a" )
36        << " punctuation character\n";
37
38     cout << "\nAccording to isprint:\n"
39        << ( isprint( '$' ) ? "$ is a" : "$ is not a" )
40        << " printing character\nAlert "
41        << ( isprint( '\a' ) ? "is a"  : "is not a" )
42        << " printing character\nSpace "
43        << ( isprint( ' ' ) ? "is a" : "is not a" )
44        << " printing character\n";
45
46     cout << "\nAccording to isgraph:\n"
47        << ( isgraph( 'Q' ) ? "Q is a" : "Q is not a" )
48        << " printing character other than a space\nSpace "
49        << ( isgraph( ' ' ) ? "is a" : "is not a" )
50        << " printing character other than a space" << endl;
51     return 0;
52  } // end main

  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



Previous Page
Next Page