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 header file.

Figure 22.17. Character-handling library functions.


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 (' '), space (' '), form feed ('f'), carriage return (' '), horizontal tab (' '), or vertical tab ('v')and false otherwise.

int iscntrl( int c )

Returns TRue if c is a control character, such as newline (' '), form feed ('f'), carriage return (' '), horizontal tab (' '), vertical tab ('v'), alert ('a'), or backspace ('')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.


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 
 4 using std::cout;
 5 using std::endl;
 6
 7 #include  // 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:
"
16 << ( isdigit( '8' ) ? "8 is a" : "8 is not a" ) << " digit
"
17 << ( isdigit( '#' ) ? "# is a" : "# is not a" ) << " digit
";
18
19 cout << "
According to isalpha:
"
20 << ( isalpha( 'A' ) ? "A is a" : "A is not a" ) << " letter
"
21 << ( isalpha( 'b' ) ? "b is a" : "b is not a" ) << " letter
"
22 << ( isalpha( '&' ) ? "& is a" : "& is not a" ) << " letter
"
23 << ( isalpha( '4' ) ? "4 is a" : "4 is not a" ) << " letter
";
24
25 cout << "
According to isalnum:
"
26 << ( isalnum( 'A' ) ? "A is a" : "A is not a" )
27 << " digit or a letter
"
28 << ( isalnum( '8' ) ? "8 is a" : "8 is not a" )
29 << " digit or a letter
"
30 << ( isalnum( '#' ) ? "# is a" : "# is not a" )
31 << " digit or a letter
";
32
33 cout << "
According to isxdigit:
"
34 << ( isxdigit( 'F' ) ? "F is a" : "F is not a" )
35 << " hexadecimal digit
"
36 << ( isxdigit( 'J' ) ? "J is a" : "J is not a" )
37 << " hexadecimal digit
"
38 << ( isxdigit( '7' ) ? "7 is a" : "7 is not a" )
39 << " hexadecimal digit
"
40 << ( isxdigit( '$' ) ? "$ is a" : "$ is not a" )
41 << " hexadecimal digit
"
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
 

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.


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 
 4 using std::cout;
 5 using std::endl;
 6
 7 #include  // 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:
"
16 << ( islower( 'p' ) ? "p is a" : "p is not a" )
17 << " lowercase letter
"
18 << ( islower( 'P' ) ? "P is a" : "P is not a" )
19 << " lowercase letter
"
20 << ( islower( '5' ) ? "5 is a" : "5 is not a" )
21 << " lowercase letter
"
22 << ( islower( '!' ) ? "! is a" : "! is not a" )
23 << " lowercase letter
";
24
25 cout << "
According to isupper:
"
26 << ( isupper( 'D' ) ? "D is an" : "D is not an" )
27 << " uppercase letter
"
28 << ( isupper( 'd' ) ? "d is an" : "d is not an" )
29 << " uppercase letter
"
30 << ( isupper( '8' ) ? "8 is an" : "8 is not an" )
31 << " uppercase letter
"
32 << ( isupper( '$' ) ? "$ is an" : "$ is not an" )
33 << " uppercase letter
";
34
35 cout << "
u converted to uppercase is "
36 << static_cast< char >( toupper( 'u' ) )
37 << "
7 converted to uppercase is "
38 << static_cast< char >( toupper( '7' ) )
39 << "
$ converted to uppercase is "
40 << static_cast< char >( toupper( '$' ) )
41 << "
L 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
 

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 (' '), 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; however, the space character is not included.


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 
 4 using std::cout;
 5 using std::endl;
 6
 7 #include  // 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:
Newline "
17 << ( isspace( '
' ) ? "is a" : "is not a" )
18 << " whitespace character
Horizontal tab "
19 << ( isspace( '	' ) ? "is a" : "is not a" )
20 << " whitespace character
"
21 << ( isspace( '%' ) ? "% is a" : "% is not a" )
22 << " whitespace character
";
23
24 cout << "
According to iscntrl:
Newline "
25 << ( iscntr1( '
' ) ? "is a" : "is not a" )
26 << " control character
"
27 << ( iscntr1( '$' ) ? "$ is a" : "$ is not a" )
28 << " control character
";
29
30 cout << "
According to ispunct:
"
31 << ( ispunct( ';' ) ? "; is a" : "; is not a" )
32 << " punctuation character
"
33 << ( ispunct( 'Y' ) ? "Y is a" : "Y is not a" )
34 << " punctuation character
"
35 << ( ispunct( '#' ) ? "# is a" : "# is not a" )
36 << " punctuation character
";
37
38 cout << "
According to isprint:
"
39 << ( isprint( '$' ) ? "$ is a" : "$ is not a" )
40 << " printing character
Alert "
41 << ( isprint( 'a' ) ? "is a" : "is not a" )
42 << " printing character
Space "
43 << ( isprint( ' ' ) ? "is a" : "is not a" )
44 << " printing character
";
45
46 cout << "
According to isgraph:
"
47 << ( isgraph( 'Q' ) ? "Q is a" : "Q is not a" )
48 << " printing character other than a space
Space "
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
 


Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net