Chapter 18 Bits Characters Strings and Structures Outline

Chapter 18 - Bits, Characters, Strings and Structures Outline 18. 1 18. 2 18. 3 18. 4 18. 5 18. 6 18. 7 18. 8 18. 9 18. 10 18. 11 18. 12 Introduction Structure Definitions Initializing Structures Using Structures with Functions typedef Example: High-Performance Card-Shuffling and Dealing Simulation Bitwise Operators Bit Fields Character-Handling Library String-Conversion Functions Search Functions of the String-Handling Library Memory Functions of the String-Handling Library 2003 Prentice Hall, Inc. All rights reserved. 1

2 18. 1 Introduction • Structures, bits, characters, C-style strings – C-like techniques – Useful for C++ programmers working with legacy C code • Structures – Hold variables of different data types – Similar to classes, but all data members public – Examine how to use structures • Make card shuffling simulation 2003 Prentice Hall, Inc. All rights reserved.

3 18. 2 Structure Definitions • Structure definition struct Card { char *face; char *suit; }; – Keyword struct – Card is structure name • Used to declare variable of structure type – Data/functions declared within braces • Structure members need unique names • Structure cannot contain instance of itself, only a pointer – Definition does not reserve memory – Definition ends with semicolon 2003 Prentice Hall, Inc. All rights reserved.

4 18. 2 Structure Definitions • Declaration – Declared like other variables: use structure type • Card one. Card, deck[ 52 ], *c. Ptr; – Can declare variables when define structure struct Card { char *face; char *suit; } one. Card, deck[ 52 ], *c. Ptr; 2003 Prentice Hall, Inc. All rights reserved.

5 18. 2 Structure Definitions • Structure operations – – Assignment to a structure of same type Taking address (&) Accessing members (one. Card. face) Using sizeof • Structure may not be in consecutive bytes of memory • Byte-alignment (2 or 4 bytes) may cause "holes" 2003 Prentice Hall, Inc. All rights reserved.

6 18. 2 Structure Definitions • Suppose 2 -byte boundary for structure members – Use structure with a char and an int • char in first byte • int on a 2 -byte boundary • Value in 1 -byte hole undefined Byte 0 01100001 1 2 3 0000 01100001 – Since hole undefined, structures may not compare equally • Cannot use == 2003 Prentice Hall, Inc. All rights reserved.

7 18. 3 Initializing Structures • Initializer lists (like arrays) – Card one. Card = { "Three", "Hearts" }; – Comma-separated values, enclosed in braces • If member unspecified, default of 0 • Initialize with assignment – Assign one structure to another Card three. Hearts = one. Card; – Assign members individually Card three. Hearts; three. Hearts. face = “Three”; three. Hearts. suit = “Hearts”; 2003 Prentice Hall, Inc. All rights reserved.

8 18. 4 Using Structures with Functions • Two ways to pass structures to functions – Pass entire structure – Pass individual members – Both pass call-by-value • To pass structures call-by-reference – Pass address – Pass reference to structure • To pass arrays call-by-value – Create structure with array as member – Pass the structure – Pass-by-reference more efficient 2003 Prentice Hall, Inc. All rights reserved.

9 18. 5 typedef • Keyword typedef – Makes synonyms (aliases) for previously defined data types • Does not create new type, only an alias – Creates shorter type names • Example – typedef Card *Card. Ptr; – Defines new type name Card. Ptr as synonym for type Card * • Card. Ptr my. Card. Ptr; • Card * my. Card. Ptr; 2003 Prentice Hall, Inc. All rights reserved.

18. 6 Example: High-Performance Card. Shuffling and Dealing Simulation • Pseudocode – Create Card structure – Put cards into array deck • Card deck[ 52 ]; – Shuffle the deck • Swap random cards – Deal the cards • Go through deck, print face and suit 2003 Prentice Hall, Inc. All rights reserved. 10

1 2 3 // Fig. 18. 2: fig 18_02. cpp // Card shuffling and dealing program using structures. #include <iostream> 4 5 6 7 8 9 using using 10 11 #include <iomanip> 12 13 using std: : setw; 14 15 16 #include <cstdlib> #include <ctime> 17 18 19 20 21 22 23 24 25 26 27 Outline std: : cout; std: : cin; std: : endl; std: : left; std: : right; Declare the Card structure. In functions, it is used like any other type. fig 18_02. cpp (1 of 4) // Card structure definition struct Card { char *face; char *suit; }; // end structure Card void fill. Deck( Card * const, char *[] ); void shuffle( Card * const ); void deal( Card * const ); 2003 Prentice Hall, Inc. All rights reserved. 11

28 29 30 31 32 33 34 35 int main() { Card deck[ 52 ]; char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; 36 37 srand( time( 0 ) ); 38 39 40 41 fill. Deck( deck, face, suit ); shuffle( deck ); deal( deck ); 42 43 return 0; 44 45 Outline // randomize fig 18_02. cpp (2 of 4) } // end main 46 2003 Prentice Hall, Inc. All rights reserved. 12

47 48 49 50 51 52 53 54 55 // place strings into Card structures void fill. Deck( Card * const w. Deck, char *w. Face[], char *w. Suit[] ) { for ( int i = 0; i < 52; i++ ) { w. Deck[ i ]. face = w. Face[ i % 13 ]; w. Deck[ i ]. suit = w. Suit[ i / 13 ]; } // end function fill. Deck 58 59 60 61 62 63 64 65 66 // shuffle cards void shuffle( Card * const w. Deck ) { for ( int i = 0; i < 52; i++ ) { int j = rand() % 52; Card temp = w. Deck[ i ]; w. Deck[ i ] = w. Deck[ j ]; w. Deck[ j ] = temp; 69 70 Outline } // end for 56 57 67 68 Create every face and suit. Note format for accessing a data member in an array of structs. Pick a random card in deck fig 18_02. cpp (0 -51) and swap with current (3 of 4) card. Notice the use of structure assignment. } // end for } // end function shuffle 71 2003 Prentice Hall, Inc. All rights reserved. 13

72 73 74 75 76 77 78 // deal cards void deal( Card * const w. Deck ) { for ( int i = 0; i < 52; i++ ) cout << right << setw( 5 ) << w. Deck[ i ]. face << " of " << left << setw( 8 ) << w. Deck[ i ]. suit << ( ( i + 1 ) % 2 ? 't' : 'n' ); 79 80 } // end function deal Outline fig 18_02. cpp (4 of 4) 2003 Prentice Hall, Inc. All rights reserved. 14

King Five Seven Three Ten Eight Nine Three Six Seven Jack Deuce Three Eight Deuce Ten Queen Seven Deuce Three Deuce Four Nine Ace Four of of of of of of of Clubs Diamonds Spades Clubs Hearts Diamonds Clubs Diamonds Spades Diamonds Clubs Spades Hearts Diamonds Clubs Hearts Spades Ten Jack Five King Eight Six Nine Queen Seven Jack King Four Five Ace Eight Six Nine Queen Five Jack Ace Four Six King of of of of of of of Diamonds Clubs Hearts Spades Hearts Clubs Hearts Diamonds Clubs Hearts Clubs Diamonds Spades Hearts Clubs Spades Hearts Diamonds Spades Outline fig 18_02. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 15

16 18. 7 Bitwise Operators • Data represented internally as sequences of bits – Each bit can be 0 or 1 – 8 bits form a byte • char is one byte • Other data types larger (int, long, etc. ) – Low-level software requires bit and byte manipulation • Operating systems, networking 2003 Prentice Hall, Inc. All rights reserved.

17 18. 7 Bitwise Operators • Bit operators – Many are overloaded – & (bitwise AND) • 1 if both bits 1, 0 otherwise – | (bitwise inclusive OR) • 1 if either bit 1, 0 otherwise – ^ (bitwise exclusive OR) • 1 if exactly one bit is 1, 0 otherwise • Alternatively: 1 if the bits are different – ~ (bitwise one's complement) • Flips 0 bits to 1, and vice versa 2003 Prentice Hall, Inc. All rights reserved.

18 18. 7 Bitwise Operators • Bit operators – << (left shift) • Moves all bits left by specified amount • Fills from right with 0 • 1 << SHIFTAMOUNT – >> (right shift with sign extension) • Moves bits right by specified amount • Fill from left can vary 2003 Prentice Hall, Inc. All rights reserved.

19 18. 7 Bitwise Operators • Next program – Print values in their binary representation – Example: unsigned integer 3 • 00000000 00000011 • (For a machine with 4 -byte integers) • Computer stores number in this form • Using masks – Integer value with specific bits set to 1 – Used to hide some bits while selecting others • Use with AND 2003 Prentice Hall, Inc. All rights reserved.

20 18. 7 Bitwise Operators • Mask example – Suppose we want to see leftmost bit of a number – AND with mask • 100000000 0000 (mask) • 10010101 10110000 10101100 00011000 (number) – If leftmost bit of number 1 • Bitwise AND will be nonzero (true) – Leftmost bit of result will be 1 • All other bits are "masked off" (ANDed with 0) – If leftmost bit of number 0 • Bitwise AND will be 0 (false) 2003 Prentice Hall, Inc. All rights reserved.

21 18. 7 Bitwise Operators • To print every bit – Print leftmost digit – Shift number left – Repeat • To create mask – Want mask of 1000000 … 0000 – How many bits in unsigned? • sizeof(unsigned) * 8 – Start with mask of 1 • Shift one less time (mask is already on first bit) • 1 << sizeof(unsigned) * 8 - 1 • 100000000 0000 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 18. 5: fig 18_05. cpp // Printing an unsigned integer in bits. #include <iostream> 4 5 6 7 using std: : cout; using std: : cin; using std: : endl; 8 9 #include <iomanip> 10 11 using std: : setw; 12 13 void display. Bits( unsigned ); 14 15 16 17 int main() { unsigned input. Value; // prototype 18 19 20 21 cout << "Enter an unsigned integer: " ; cin >> input. Value; display. Bits( input. Value ); 22 23 return 0; 24 25 Outline fig 18_05. cpp (1 of 2) } // end main 26 2003 Prentice Hall, Inc. All rights reserved. 22

27 28 29 30 31 // display bits of an unsigned integer value void display. Bits( unsigned value ) { const int SHIFT = 8 * sizeof( unsigned ) - 1; const unsigned MASK = 1 << SHIFT; 32 33 cout << setw( 10 ) << value << " = "; 34 35 36 37 for ( unsigned i = 1; i <= cout << ( value & MASK ? '1' : '0' ); value <<= 1; // shift value left by 1 38 39 40 SHIFT = 32 - 1 = 31 MASK = 100000000 0000 SHIFT + 1; i++ ) { if ( i % 8 == 0 ) cout << ' '; 41 42 } // end for 43 44 cout << endl; 45 46 Outline // output a space after 8 bits Shift value left by 1 to examine next bit. Note use of <<= (same as value = value << 1). Bitwise AND value and fig 18_05. cpp mask. If it is nonzero (true), (2 of 2) then the leftmost digit is a 1. fig 18_05. cpp output (1 of 1) } // end function display. Bits Enter an unsigned integer: 65000 = 00000000 11111101000 2003 Prentice Hall, Inc. All rights reserved. 23

24 18. 7 Bitwise Operators • Upcoming examples – Demo operators – & (AND) • x & y – | (OR) • x | y – ^ (Exclusive OR) • x ^ y – ~ (Complement) • ~x – << and >> (Left shift and right shift) 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 4 // Fig. 18. 7: fig 18_07. cpp // Using the bitwise AND, bitwise inclusive OR, bitwise // exclusive OR and bitwise complement operators. #include <iostream> 5 6 7 using std: : cout; using std: : cin; 8 9 #include <iomanip> 10 11 12 using std: : endl; using std: : setw; 13 14 void display. Bits( unsigned ); 15 16 17 18 19 20 21 int main() { unsigned // prototype Outline fig 18_07. cpp (1 of 4) number 1; number 2; mask; set. Bits; 22 2003 Prentice Hall, Inc. All rights reserved. 25

23 24 25 26 27 28 29 30 // demonstrate bitwise & number 1 = 2179876355; mask = 1; cout << "The result of combining the followingn" ; display. Bits( number 1 ); display. Bits( mask ); cout << "using the bitwise AND operator & isn"; display. Bits( number 1 & mask ); 31 32 33 34 35 36 37 38 39 // demonstrate bitwise | number 1 = 15; set. Bits = 241; cout << "n. The result of combining the followingn" ; display. Bits( number 1 ); display. Bits( set. Bits ); cout << "using the bitwise inclusive OR operator | isn" ; display. Bits( number 1 | set. Bits ); 40 41 42 43 44 45 46 47 48 // demonstrate bitwise exclusive OR number 1 = 139; number 2 = 199; cout << "n. The result of combining the followingn" ; display. Bits( number 1 ); display. Bits( number 2 ); cout << "using the bitwise exclusive OR operator ^ isn" ; display. Bits( number 1 ^ number 2 ); Outline fig 18_07. cpp (2 of 4) 2003 Prentice Hall, Inc. All rights reserved. 26

49 50 51 52 53 54 55 // demonstrate bitwise complement number 1 = 21845; cout << "n. The one's complement ofn"; display. Bits( number 1 ); cout << "is" << endl; display. Bits( ~number 1 ); 56 57 return 0; 58 59 } // end main 60 61 62 63 64 65 // display bits of an unsigned integer value void display. Bits( unsigned value ) { const int SHIFT = 8 * sizeof( unsigned ) - 1; const unsigned MASK = 1 << SHIFT; 66 67 cout << setw( 10 ) << value << " = "; 68 69 70 71 for ( unsigned i = 1; i <= SHIFT + 1; i++ ) { cout << ( value & MASK ? '1' : '0' ); value <<= 1; // shift value left by 1 72 73 74 75 76 if ( i % 8 == 0 ) cout << ' '; Outline fig 18_07. cpp (3 of 4) // output a space after 8 bits } // end for 2003 Prentice Hall, Inc. All rights reserved. 27

77 78 79 80 cout << endl; Outline } // end function display. Bits The result of combining the following 2179876355 = 10000001 1110 01000110 00000011 1 = 00000000 00000001 using the bitwise AND operator & is 1 = 00000000 00000001 The result of combining the following 15 = 00000000 00001111 241 = 00000000 11110001 using the bitwise inclusive OR operator | is 255 = 00000000 1111 The result of combining the following 139 = 00000000 10001011 fig 18_07. cpp (4 of 4) fig 18_07. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 28

1 2 3 // Fig. 18. 11: fig 18_11. cpp // Using the bitwise shift operators. #include <iostream> 4 5 6 7 using std: : cout; using std: : cin; using std: : endl; 8 9 #include <iomanip> 10 11 using std: : setw; 12 13 void display. Bits( unsigned ); 14 15 16 17 int main() { unsigned number 1 = 960; 18 19 20 21 22 23 24 // prototype Outline fig 18_07. cpp (1 of 3) // demonstrate bitwise left shift cout << "The result of left shiftingn" ; display. Bits( number 1 ); cout << "8 bit positions using the left " << "shift operator isn"; display. Bits( number 1 << 8 ); 25 2003 Prentice Hall, Inc. All rights reserved. 29

26 27 28 29 30 31 // demonstrate bitwise right shift cout << "n. The result of right shiftingn" ; display. Bits( number 1 ); cout << "8 bit positions using the right " << "shift operator isn"; display. Bits( number 1 >> 8 ); 32 33 return 0; 34 35 } // end main 36 37 38 39 40 41 // display bits of an unsigned integer value void display. Bits( unsigned value ) { const int SHIFT = 8 * sizeof( unsigned ) - 1; const unsigned MASK = 1 << SHIFT; 42 43 cout << setw( 10 ) << value << " = "; 44 45 46 47 for ( unsigned i = 1; i <= SHIFT + 1; i++ ) { cout << ( value & MASK ? '1' : '0' ); value <<= 1; // shift value left by 1 48 49 50 51 52 if ( i % 8 == 0 ) cout << ' '; Outline fig 18_07. cpp (2 of 3) // output a space after 8 bits } // end for 2003 Prentice Hall, Inc. All rights reserved. 30

53 54 55 56 cout << endl; Outline } // end function display. Bits The result of left shifting 960 = 0000000011 11000000 8 bit positions using the left shift operator is 245760 = 00000011 11000000 The result of right shifting 960 = 0000000011 11000000 8 bit positions using the right shift operator is 3 = 00000000 00000011 fig 18_07. cpp (3 of 3) fig 18_07. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 31

32 18. 8 Bit Fields • Bit field – – Member of structure whose size (in bits) has been specified Enables better memory utilization Must be declared int or unsigned Example Struct Bit. Card { unsigned face : 4; unsigned suit : 2; unsigned color : 1; }; – Declare with name : width • Bit width must be an integer 2003 Prentice Hall, Inc. All rights reserved.

33 18. 8 Bit Fields • Accessing bit fields – Access like any other structure member Struct Bit. Card { unsigned face : 4; unsigned suit : 2; unsigned color : 1; }; – my. Card. face = 10; • face has 4 bits, can store values 0 - 15 • suit can store 0 - 3 • color can store 0 or 1 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 18. 14: fig 18_14. cpp // Representing cards with bit fields in a struct. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 13 14 15 16 17 18 Outline Declare bit fields inside a structure to store card data. // Bit. Card structure definition with bit fields struct Bit. Card { unsigned face : 4; // 4 bits; 0 -15 unsigned suit : 2; // 2 bits; 0 -3 unsigned color : 1; // 1 bit; 0 -1 fig 18_14. cpp (1 of 3) }; // end struct Bit. Bard 19 20 21 void fill. Deck( Bit. Card * const ); void deal( const Bit. Card * const ); 22 23 24 25 int main() { Bit. Card deck[ 52 ]; 26 27 28 fill. Deck( deck ); deal( deck ); // prototype 2003 Prentice Hall, Inc. All rights reserved. 34

29 30 31 32 } // end main 33 34 35 36 37 38 39 40 // initialize Bit. Cards void fill. Deck( Bit. Card * const w. Deck ) { for ( int i = 0; i <= 51; i++ ) { w. Deck[ i ]. face = i % 13; w. Deck[ i ]. suit = i / 13; w. Deck[ i ]. color = i / 26; 41 42 43 44 Outline return 0; } // end for Assign to bit fields as normal, but be careful of each field's range. fig 18_14. cpp (2 of 3) } // end function fill. Deck 45 2003 Prentice Hall, Inc. All rights reserved. 35

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 // output cards in two column format; cards 0 -25 subscripted // with k 1 (column 1); cards 26 -51 subscripted k 2 (column 2) void deal( const Bit. Card * const w. Deck ) { for ( int k 1 = 0, k 2 = k 1 + 26; k 1 <= 25; k 1++, k 2++ ) { cout << "Card: " << setw( 3 ) << w. Deck[ k 1 ]. face << " Suit: " << setw( 2 ) << w. Deck[ k 1 ]. suit << " Color: " << setw( 2 ) << w. Deck[ k 1 ]. color << " " << "Card: " << setw( 3 ) << w. Deck[ k 2 ]. face << " Suit: " << setw( 2 ) << w. Deck[ k 2 ]. suit << " Color: " << setw( 2 ) << w. Deck[ k 2 ]. color << endl; } // end for Outline fig 18_14. cpp (3 of 3) } // end function deal 2003 Prentice Hall, Inc. All rights reserved. 36

Card: Card: Card: Card: Card: Card: Card: 0 1 2 3 4 5 6 7 8 9 10 11 12 Suit: Suit: Suit: Suit: Suit: Suit: Suit: 0 0 0 0 1 1 1 1 Color: Color: Color: Color: Color: Color: Color: 0 0 0 0 0 0 0 Card: Card: Card: Card: Card: Card: Card: 0 1 2 3 4 5 6 7 8 9 10 11 12 Suit: Suit: Suit: Suit: Suit: Suit: Suit: 2 2 2 2 3 3 3 3 Color: Color: Color: Color: Color: Color: Color: 1 1 1 1 1 1 1 Outline fig 18_14. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 37

38 18. 8 Bit Fields • Other notes – Bit fields are not arrays of bits (cannot use []) – Cannot take address of bit fields – Use unnamed bit fields to pad structure Struct Example unsigned a : unsigned b : }; { 13; 3; 4; – Use unnamed, zero-width fields to align to boundary Struct Example unsigned a : unsigned b : }; { 13; 0; 4; • Automatically aligns b to next boundary 2003 Prentice Hall, Inc. All rights reserved.

39 18. 9 Character-Handling Library • Character Handling Library – <cctype> – Functions to perform tests and manipulations on characters – Pass character as argument • Character represented by an int – char does not allow negative values • Characters often manipulated as ints • EOF usually has value -1 2003 Prentice Hall, Inc. All rights reserved.

40 18. 9 Character-Handling Library • Upcoming example – isalpha( int c ) • (All character functions take int argument) • Returns true if c is a letter (A-Z, a-z) • Returns false otherwise – isdigit • Returns true if digit (0 -9) – isalnum • Returns true if letter or digit (A-Z, a-z, 0 -9) – isxdigit • Returns true if hexadecimal digit (A-F, a-f, 0 -9) 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 18. 17: fig 18_17. cpp // Using functions isdigit, isalpha, isalnum and isxdigit. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cctype> 9 10 11 12 13 14 15 16 int main() { cout << << << 17 18 19 20 21 22 23 24 25 26 cout << << << Outline Note use of conditional operator: // character-handling function prototypes condition ? value if true : value if false "According to isdigit: n" ( isdigit( '8' ) ? "8 is a" : "8 is not a" ) " digitn" ( isdigit( '#' ) ? "# is a" : "# is not a" ) " digitn"; "n. According to isalpha: n" ( isalpha( 'A' ) ? "A is a" " lettern" ( isalpha( 'b' ) ? "b is a" " lettern" ( isalpha( '&' ) ? "& is a" " lettern" ( isalpha( '4' ) ? "4 is a" " lettern"; fig 18_17. cpp (1 of 2) : "A is not a" ) : "b is not a" ) : "& is not a" ) : "4 is not a" ) 27 2003 Prentice Hall, Inc. All rights reserved. 41

28 29 30 31 32 33 34 cout << << "n. According to isalnum: n" ( isalnum( 'A' ) ? "A is a" : "A is not a" ) " digit or a lettern" ( isalnum( '8' ) ? "8 is a" : "8 is not a" ) " digit or a lettern" ( isalnum( '#' ) ? "# is a" : "# is not a" ) " digit or a lettern"; 35 36 37 38 39 40 41 42 43 44 45 46 cout << << << "n. According to isxdigit: n" ( isxdigit( 'F' ) ? "F is a" : " hexadecimal digitn" ( isxdigit( 'J' ) ? "J is a" : " hexadecimal digitn" ( isxdigit( '7' ) ? "7 is a" : " hexadecimal digitn" ( isxdigit( '$' ) ? "$ is a" : " hexadecimal digitn" ( isxdigit( 'f' ) ? "f is a" : " hexadecimal digit" << endl; 47 48 return 0; 49 50 Outline "F is not a" ) "J is not a" ) "7 is not a" ) fig 18_17. cpp (2 of 2) "$ is not a" ) "f is not a" ) } // end main 2003 Prentice Hall, Inc. All rights reserved. 42

According to isdigit: 8 is a digit # is not a digit Outline 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 fig 18_17. cpp output (1 of 1) 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 2003 Prentice Hall, Inc. All rights reserved. 43

44 18. 9 Character-Handling Library • Upcoming example – islower • Returns true if lowercase letter (a-z) – isupper • Returns true if uppercase letter (A-Z) – tolower • If passed uppercase letter, returns lowercase letter – A to a • Otherwise, returns original argument – toupper • As above, but turns lowercase letter to uppercase – a to A 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 18: fig 18_18. cpp // Using functions islower, isupper, tolower and toupper. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cctype> 9 10 11 12 13 14 15 16 17 18 19 20 int main() { cout << << << 21 22 23 24 25 26 27 28 cout << << Outline // character-handling function prototypes "According to islower: n" ( islower( 'p' ) ? "p is a" " lowercase lettern" ( islower( 'P' ) ? "P is a" " lowercase lettern" ( islower( '5' ) ? "5 is a" " lowercase lettern" ( islower( '!' ) ? "! is a" " lowercase lettern"; : "p is not a" ) fig 18_18. cpp (1 of 2) : "P is not a" ) : "5 is not a" ) : "! is not a" ) "n. According to isupper: n" ( isupper( 'D' ) ? "D is an" : "D is not an" ) " uppercase lettern" ( isupper( 'd' ) ? "d is an" : "d is not an" ) " uppercase lettern" ( isupper( '8' ) ? "8 is an" : "8 is not an" ) " uppercase lettern" 2003 Prentice Hall, Inc. All rights reserved. 45

29 30 << ( isupper( '$' ) ? "$ is an" : "$ is not an" ) << " uppercase lettern"; 31 32 33 34 35 36 37 38 39 cout << << 40 41 return 0; 42 43 "nu converted to static_cast< char "n 7 converted to static_cast< char "n$ converted to static_cast< char "n. L converted to static_cast< char uppercase is " >( toupper( 'u' uppercase is " >( toupper( '7' uppercase is " >( toupper( '$' lowercase is " >( tolower( 'L' Outline ) ) ) ) << endl; fig 18_18. cpp (2 of 2) } // end main 2003 Prentice Hall, Inc. All rights reserved. 46

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 Outline 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 7 $ L converted to to uppercase lowercase is is U 7 $ l fig 18_18. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 47

48 18. 9 Character-Handling Library • Upcoming example – isspace • Returns true if space ' ', form feed 'f', newline 'n', carriage return 'r', horizontal tab 't', vertical tab 'v' – iscntrl • Returns true if control character, such as tabs, form feed, alert ('a'), backspace('b'), carriage return, newline – ispunct • Returns true if printing character other than space, digit, or letter • $ # ( ) [ ] { } ; : %, etc. 2003 Prentice Hall, Inc. All rights reserved.

49 18. 9 Character-Handling Library • Upcoming example – isprint • Returns true if character can be displayed (including space) – isgraph • Returns true if character can be displayed, not including space 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 18. 19: fig 18_19. cpp // Using functions isspace, iscntrl, ispunct, isprint, isgraph. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cctype> 9 10 11 12 13 14 15 16 17 18 int main() { cout << << "According to isspace: n. Newline " ( isspace( 'n' ) ? "is a" : "is not a" ) " whitespace charactern. Horizontal tab " ( isspace( 't' ) ? "is a" : "is not a" ) " whitespace charactern" ( isspace( '%' ) ? "% is a" : "% is not a" ) " whitespace charactern"; 19 20 21 22 23 24 cout << << << "n. According to iscntrl: n. Newline " ( iscntrl( 'n' ) ? "is a" : "is not a" ) " control charactern" ( iscntrl( '$' ) ? "$ is a" : "$ is not a" ) " control charactern"; Outline // character-handling function prototypes fig 18_19. cpp (1 of 2) 25 2003 Prentice Hall, Inc. All rights reserved. 50

26 27 28 29 30 31 32 cout << << "n. According to ispunct: n" ( ispunct( '; ' ) ? "; is a" : "; is not a" ) " punctuation charactern" ( ispunct( 'Y' ) ? "Y is a" : "Y is not a" ) " punctuation charactern" ( ispunct( '#' ) ? "# is a" : "# is not a" ) " punctuation charactern" ; 33 34 35 36 37 38 cout << << << "n. According to isprint: n" ( isprint( '$' ) ? "$ is a" : "$ is not a" ) " printing charactern. Alert " ( isprint( 'a' ) ? "is a" : "is not a" ) " printing charactern"; 39 40 41 42 43 44 cout << << << "n. According to isgraph: n" ( isgraph( 'Q' ) ? "Q is a" : "Q is not a" ) " printing character other than a space n. Space " ( isgraph( ' ' ) ? "is a" : "is not a" ) " printing character other than a space" << endl; 45 46 return 0; 47 48 Outline fig 18_19. cpp (2 of 2) } // end main 2003 Prentice Hall, Inc. All rights reserved. 51

According to isspace: Newline is a whitespace character Horizontal tab is a whitespace character % is not a whitespace character Outline 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 fig 18_19. cpp output (1 of 1) According to isprint: $ is a printing character Alert is not 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 2003 Prentice Hall, Inc. All rights reserved. 52

53 18. 10 String-Conversion Functions • String conversion functions – Convert to numeric values, searching, comparison – <cstdlib> – Most functions take const char * • Do not modify string 2003 Prentice Hall, Inc. All rights reserved.

54 18. 10 String-Conversion Functions • Functions – double atof( const char *n. Ptr ) • Converts string to floating point number (double) • Returns 0 if cannot be converted – int atoi( const char *n. Ptr ) • Converts string to integer • Returns 0 if cannot be converted – long atol( const char *n. Ptr ) • Converts string to long integer • If int and long both 4 -bytes, then atoi and atol identical 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 18. 21: fig 18_21. cpp // Using atof. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstdlib> 9 10 11 12 int main() { double d = atof( "99. 0" ); // atof prototype 13 14 15 16 cout << "The string "99. 0" converted to double is " << d << "n. The converted value divided by 2 is " << d / 2. 0 << endl; 17 18 return 0; 19 20 Outline fig 18_21. cpp (1 of 1) fig 18_21. cpp output (1 of 1) } // end main The string "99. 0" converted to double is 99 The converted value divided by 2 is 49. 5 2003 Prentice Hall, Inc. All rights reserved. 55

1 2 3 // Fig. 18. 22: fig 18_22. cpp // Using atoi. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstdlib> 9 10 11 12 int main() { int i = atoi( "2593" ); // atoi prototype 13 14 15 16 cout << "The string "2593" converted to int is " << i << "n. The converted value minus 593 is " << i - 593 << endl; 17 18 return 0; 19 20 Outline fig 18_22. cpp (1 of 1) fig 18_22. cpp output (1 of 1) } // end main The string "2593" converted to int is 2593 The converted value minus 593 is 2000 2003 Prentice Hall, Inc. All rights reserved. 56

1 2 3 // Fig. 18. 23: fig 18_23. cpp // Using atol. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstdlib> 9 10 11 12 int main() { long x = atol( "1000000" ); // atol prototype 13 14 15 16 cout << "The string "1000000" converted to long is " << x << "n. The converted value divided by 2 is " << x / 2 << endl; 17 18 return 0; 19 20 Outline fig 18_23. cpp (1 of 1) fig 18_23. cpp output (1 of 1) } // end main The string "1000000" converted to long int is 1000000 The converted value divided by 2 is 500000 2003 Prentice Hall, Inc. All rights reserved. 57

58 18. 10 String-Conversion Functions • Functions – double strtod( const char *n. Ptr, char **end. Ptr ) • Converts first argument to double, returns that value • Sets second argument to location of first character after converted portion of string • strtod("123. 4 this is a test", &string. Ptr); – Returns 123. 4 – string. Ptr points to "this is a test" 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 18. 24: fig 18_24. cpp // Using strtod. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstdlib> 9 10 11 12 13 14 int main() { double d; const char *string 1 = "51. 2% are admitted"; char *string. Ptr; // strtod prototype 15 16 d = strtod( string 1, &string. Ptr ); 17 18 19 20 cout << "The string "" << string 1 << "" is converted to the ndouble value " << d << " and the string "" << string. Ptr << """ << endl; 21 22 return 0; 23 24 Outline fig 18_24. cpp (1 of 1) fig 18_24. cpp output (1 of 1) } // end main The string "51. 2% are admitted" is converted to the double value 51. 2 and the string "% are admitted" 2003 Prentice Hall, Inc. All rights reserved. 59

60 18. 10 String-Conversion Functions • Functions – long strtol( const char *n. Ptr, char **end. Ptr, int base ) • Converts first argument to long, returns that value • Sets second argument to location of first character after converted portion of string – If NULL, remainder of string ignored • Third argument is base of value being converted – Any number 2 - 36 – 0 specifies octal, decimal, or hexadecimal – long strtoul • As above, with unsigned long 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 18. 25: fig 18_25. cpp // Using strtol. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstdlib> 9 10 11 12 13 14 int main() { long x; const char *string 1 = "-1234567 abc"; char *remainder. Ptr; // strtol prototype 15 16 x = strtol( string 1, &remainder. Ptr, 0 ); 17 18 19 20 21 22 23 cout << << << 24 25 return 0; 26 27 Outline fig 18_25. cpp (1 of 1) "The original string is "" << string 1 ""n. The converted value is " << x "n. The remainder of the original string is "" remainder. Ptr ""n. The converted value plus 567 is " x + 567 << endl; } // end main 2003 Prentice Hall, Inc. All rights reserved. 61

The The original string is "-1234567 abc" converted value is -1234567 remainder of the original string is " abc" converted value plus 567 is -1234000 Outline fig 18_25. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 62

1 2 3 // Fig. 18. 26: fig 18_26. cpp // Using strtoul. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstdlib> 9 10 11 12 13 14 int main() { unsigned long x; const char *string 1 = "1234567 abc"; char *remainder. Ptr; // strtoul prototype 15 16 x = strtoul( string 1, &remainder. Ptr, 0 ); 17 18 19 20 21 22 23 cout << << << 24 25 return 0; 26 27 Outline fig 18_26. cpp (1 of 1) "The original string is "" << string 1 ""n. The converted value is " << x "n. The remainder of the original string is "" remainder. Ptr ""n. The converted value minus 567 is " x - 567 << endl; } // end main 2003 Prentice Hall, Inc. All rights reserved. 63

The The original string is "1234567 abc" converted value is 1234567 remainder of the original string is " abc" converted value minus 567 is 1234000 Outline fig 18_26. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 64

18. 11 Search Functions of the String. Handling Library • String handling library – Search strings for characters, other strings – Type size_t • Defined as integer of type returned by sizeof 2003 Prentice Hall, Inc. All rights reserved. 65

18. 11 Search Functions of the String. Handling Library • Functions – char *strchr( const char *s, int c ) • Returns pointer to first occurrence of c in s • Returns NULL if not found 2003 Prentice Hall, Inc. All rights reserved. 66

1 2 3 // Fig. 18. 28: fig 18_28. cpp // Using strchr. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 14 int main() { const char *string 1 = "This is a test"; character 1 = 'a'; character 2 = 'z'; Outline // strchr prototype 15 16 17 18 19 20 21 if ( strchr( string 1, character 1 ) != NULL ) cout << ''' << character 1 << "' was found in "" << string 1 << "". n"; else cout << ''' << character 1 << "' was not found in "" << string 1 << "". n"; 22 23 24 25 26 27 28 if ( strchr( string 1, character 2 ) != NULL ) cout << ''' << character 2 << "' was found in "" << string 1 << "". n"; else cout << ''' << character 2 << "' was not found in "" << string 1 << "". " << endl; fig 18_28. cpp (1 of 2) 2003 Prentice Hall, Inc. All rights reserved. 67

29 30 31 32 return 0; Outline } // end main 'a' was found in "This is a test". 'z' was not found in "This is a test". fig 18_28. cpp (2 of 2) fig 18_28. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 68

18. 11 Search Functions of the String. Handling Library • Functions – size_t strcspn( const char *s 1, const char *s 2 ) • Returns length of s 1 that does not contain characters in s 2 • Starts from beginning of s 1 – char *strpbrk( const char *s 1, const char *s 2 ) • Finds first occurrence of any character in s 2 in s 1 • Returns NULL if not found – char *strrchr( const char *s, int c ) • Returns pointer to last occurrence of c in s • Returns NULL if not found 2003 Prentice Hall, Inc. All rights reserved. 69

1 2 3 // Fig. 18. 29: fig 18_29. cpp // Using strcspn. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 int main() { const char *string 1 = "The value is 3. 14159"; const char *string 2 = "1234567890"; 14 15 16 17 18 cout << << 19 20 return 0; 21 22 Outline // strcspn prototype "string 1 = " << string 1 << "nstring 2 = " << string 2 "nn. The length of the initial segment of string 1" "ncontaining no characters from string 2 = " strcspn( string 1, string 2 ) << endl; fig 18_29. cpp (1 of 1) fig 18_29. cpp output (1 of 1) } // end main string 1 = The value is 3. 14159 string 2 = 1234567890 The length of the initial segment of string 1 2003 Prentice Hall, Inc. All rights reserved. 70

1 2 3 // Fig. 18. 30: fig 18_30. cpp // Using strpbrk. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 int main() { const char *string 1 = "This is a test"; const char *string 2 = "beware"; 14 15 16 17 18 cout << << 19 20 return 0; 21 22 Outline // strpbrk prototype "Of the characters in "" << string 2 << ""n'" *strpbrk( string 1, string 2 ) << ''' " is the first character to appear inn"" string 1 << '"' << endl; fig 18_30. cpp (1 of 1) fig 18_30. cpp output (1 of 1) } // end main Of the characters in "beware" 'a' is the first character to appear in "This is a test" 2003 Prentice Hall, Inc. All rights reserved. 71

1 2 3 // Fig. 18. 31: fig 18_31. cpp // Using strrchr. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 14 int main() { const char *string 1 = "A zoo has many animals including zebras" ; int c = 'z'; 15 16 17 18 19 cout << << 20 21 return 0; 22 23 Outline // strrchr prototype "The remainder of string 1 beginning with then" "last occurrence of character '" static_cast< char >( c ) // print as char not int "' is: "" << strrchr( string 1, c ) << '"' << endl; fig 18_31. cpp (1 of 1) fig 18_31. cpp output (1 of 1) } // end main The remainder of string 1 beginning with the last occurrence of character 'z' is: "zebras" 2003 Prentice Hall, Inc. All rights reserved. 72

18. 11 Search Functions of the String. Handling Library • Functions – size_t strspn( const char *s 1, const char *s 2 ) • Returns length of s 1 that contains only characters in s 2 • Starts from beginning of s 1 – char *strstr( const char *s 1, const char *s 2 ) • Finds first occurrence of s 2 in s 1 • Returns NULL if not found 2003 Prentice Hall, Inc. All rights reserved. 73

1 2 3 // Fig. 18. 32: fig 18_32. cpp // Using strspn. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 int main() { const char *string 1 = "The value is 3. 14159"; const char *string 2 = "aehils Tuv"; 14 15 16 17 18 19 cout << << << 20 21 return 0; 22 23 Outline // strspn prototype fig 18_32. cpp (1 of 1) "string 1 = " << string 1 "nstring 2 = " << string 2 "nn. The length of the initial segment of string 1n" "containing only characters from string 2 = " strspn( string 1, string 2 ) << endl; } // end main 2003 Prentice Hall, Inc. All rights reserved. 74

string 1 = The value is 3. 14159 string 2 = aehils Tuv Outline The length of the initial segment of string 1 containing only characters from string 2 = 13 fig 18_32. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 75

1 2 3 // Fig. 18. 33: fig 18_33. cpp // Using strstr. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 int main() { const char *string 1 = "abcdef"; const char *string 2 = "def"; 14 15 16 17 18 cout << << 19 20 return 0; 21 22 Outline // strstr prototype fig 18_33. cpp (1 of 1) "string 1 = " << string 1 << "nstring 2 = " << string 2 "nn. The remainder of string 1 beginning with then" "first occurrence of string 2 is: " strstr( string 1, string 2 ) << endl; } // end main 2003 Prentice Hall, Inc. All rights reserved. 76

string 1 = abcdef string 2 = def Outline The remainder of string 1 beginning with the first occurrence of string 2 is: defabcdef fig 18_33. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 77

18. 12 Memory Functions of the String. Handling Library • Memory functions – – Treat memory as array of characters Manipulate any block of data Treat pointers as void * Specify size (number of bytes) 2003 Prentice Hall, Inc. All rights reserved. 78

18. 12 Memory Functions of the String. Handling Library • Functions – void *memcpy( void *s 1, const void *s 2, size_t n ) • Copies n characters from s 2 to s 1 • Do not use if s 2 and s 1 overlap • Returns pointer to result – void *memmove( void *s 1, const void *s 2, size_t n ) • Copies n characters from s 2 to s 1 • Ok if objects overlap • Returns pointer to result 2003 Prentice Hall, Inc. All rights reserved. 79

1 2 3 // Fig. 18. 35: fig 18_35. cpp // Using memcpy. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 13 int main() { char s 1[ 17 ]; char s 2[] = "Copy this string"; // memcpy prototype 14 15 memcpy( s 1, s 2, 17 ); 16 17 18 cout << "After s 2 is copied into s 1 with memcpy, n" << "s 1 contains "" << s 1 << '"' << endl; 19 20 return 0; 21 22 Outline fig 18_35. cpp (1 of 1) fig 18_35. cpp output (1 of 1) } // end main After s 2 is copied into s 1 with memcpy, s 1 contains "Copy this string" 2003 Prentice Hall, Inc. All rights reserved. 80

1 2 3 // Fig. 18. 36: fig 18_36. cpp // Using memmove. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 int main() { char x[] = "Home Sweet Home"; // memmove prototype 13 14 15 16 17 cout << "The string in array x before memmove is: " << x; cout << "n. The string in array x after memmove is: " << static_cast< char * >( memmove( x, &x[ 5 ], 10 ) ) << endl; 18 19 return 0; 20 21 Outline fig 18_36. cpp (1 of 1) fig 18_36. cpp output (1 of 1) } // end main The string in array x before memmove is: Home Sweet Home The string in array x after memmove is: Sweet Home 2003 Prentice Hall, Inc. All rights reserved. 81

18. 12 Memory Functions of the String. Handling Library • Functions – int memcmp( const void *s 1, const void *s 2, size_t n ) • Compares first n characters of s 1 and s 2 • Returns – 0 (equal) – Greater than 0 (s 1 > s 2) – Less than 0 (s 1 < s 2) 2003 Prentice Hall, Inc. All rights reserved. 82

1 2 3 // Fig. 18. 37: fig 18_37. cpp // Using memcmp. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 #include <cstring> 13 14 15 16 17 int main() { char s 1[] = "ABCDEFG"; char s 2[] = "ABCDXYZ"; 18 19 20 21 22 23 24 cout << << << 25 26 return 0; 27 28 // memcmp prototype Outline fig 18_37. cpp (1 of 1) "s 1 = " << s 1 << "ns 2 = " << s 2 << endl "nmemcmp(s 1, s 2, 4) = " << setw( 3 ) memcmp( s 1, s 2, 4 ) << "nmemcmp(s 1, s 2, 7) = " setw( 3 ) << memcmp( s 1, s 2, 7 ) "nmemcmp(s 2, s 1, 7) = " << setw( 3 ) memcmp( s 2, s 1, 7 ) << endl; } // end main 2003 Prentice Hall, Inc. All rights reserved. 83

s 1 = ABCDEFG s 2 = ABCDXYZ Outline memcmp(s 1, s 2, 4) = 0 memcmp(s 1, s 2, 7) = -1 memcmp(s 2, s 1, 7) = 1 fig 18_37. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 84

18. 12 Memory Functions of the String. Handling Library • Functions – void *memchr(const void *s, int c, size_t n ) • Finds first occurrence of c in first n characters of s • Returns pointer to c or NULL – void *memset( void *s, int c, size_t n ) • Copies c into first n characters of s • Returns pointer to result 2003 Prentice Hall, Inc. All rights reserved. 85

1 2 3 // Fig. 18. 38: fig 18_38. cpp // Using memchr. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 int main() { char s[] = "This is a string"; 13 14 15 16 17 cout << << 18 19 return 0; 20 21 Outline // memchr prototype "The remainder of s after character 'r' " "is found is "" static_cast< char * >( memchr( s, 'r', 16 ) ) '"' << endl; fig 18_38. cpp (1 of 1) fig 18_38. cpp output (1 of 1) } // end main The remainder of s after character 'r' is found is "ring" 2003 Prentice Hall, Inc. All rights reserved. 86

1 2 3 // Fig. 18. 39: fig 18_39. cpp // Using memset. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> 9 10 11 12 int main() { char string 1[ 15 ] = "BBBBBBB"; // memset prototype 13 14 15 16 17 cout << "string 1 = " << string 1 << endl; cout << "string 1 after memset = " << static_cast< char * >( memset( string 1, 'b', 7 ) ) << endl; 18 19 return 0; 20 21 Outline fig 18_39. cpp (1 of 1) fig 18_39. cpp output (1 of 1) } // end main string 1 = BBBBBBB string 1 after memset = bbbbbbb. BBBBBBB 2003 Prentice Hall, Inc. All rights reserved. 87
- Slides: 87