Professional Documents
Culture Documents
Outline 18.1 18.2 18.3 18.4 18.5 18.6 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
Structures
Hold variables of different data types Similar to classes, but all data members public Examine how to use structures
Make card shuffling simulation
18.4 Using Structures with Functions Two ways to pass structures to functions
Pass entire structure Pass individual members Both pass call-by-value
Example
typedef Card *CardPtr; Defines new type name CardPtr as synonym for type Card *
CardPtr myCardPtr; Card * myCardPtr;
10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Fig. 18.2: fig18_02.cpp // Card shuffling and dealing program using structures. #include <iostream> using using using using using std::cout; std::cin; std::endl; std::left; std::right;
11
Outline
fig18_02.cpp (1 of 4)
#include <iomanip> using std::setw; #include <cstdlib> #include <ctime> // Card structure definition struct Card { char *face; char *suit;
Declare the Card structure. In functions, it is used like any other type.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
12
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" };
Outline
fig18_02.cpp (2 of 4)
srand( time( 0 ) );
// randomize
fillDeck( deck, face, suit ); shuffle( deck ); deal( deck ); return 0; } // end main
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
// place strings into Card structures void fillDeck( Card * const wDeck, char *wFace[], char *wSuit[] ) { for ( int i = 0; i < 52; i++ ) { wDeck[ i ].face = wFace[ i % 13 ]; wDeck[ i ].suit = wSuit[ i / 13 ]; } // end for } // end function fillDeck // shuffle cards void shuffle( Card * const wDeck ) { for ( int i = 0; i < 52; i++ ) { int j = rand() % 52; Card temp = wDeck[ i ]; wDeck[ i ] = wDeck[ j ]; wDeck[ j ] = temp; } // end for } // end function shuffle
13
Create every face and suit. Note format for accessing a data member in an array of fig18_02.cpp structs. (3 of 4)
Outline
Pick a random card in deck (0-51) and swap with current card. Notice the use of structure assignment.
72 73 74 75 76 77 78 79 80
// deal cards void deal( Card * const wDeck ) { for ( int i = 0; i < 52; i++ ) cout << right << setw( 5 ) << wDeck[ i ].face << " of " << left << setw( 8 ) << wDeck[ i ].suit << ( ( i + 1 ) % 2 ? '\t' : '\n' ); } // end function deal
14
Outline
fig18_02.cpp (4 of 4)
King Five Seven Three Ten Eight Nine Three Six Seven Jack Deuce Three Eight Deuce Ten Ten Queen Seven Deuce Three Deuce Four Nine Ace Four
of of of of of of of of of of of of of of of of of of of of of of of of of of
Clubs Diamonds Spades Spades Clubs Hearts Diamonds Diamonds Clubs Diamonds Spades Diamonds Clubs Clubs Spades Spades Hearts Diamonds Clubs Clubs Hearts Hearts Hearts Spades Spades Spades
Ten Jack Five King Eight Six Nine Queen Seven Jack King Four Five Ace Ace Eight Six Nine Queen Queen Five Jack Ace Four Six King
of of of of of of of of of of of of of of of of of of of of of of of of of of
Diamonds Clubs Clubs Hearts Spades Hearts Clubs Hearts Hearts Diamonds Diamonds Clubs Hearts Hearts Clubs Diamonds Spades Hearts Clubs Spades Spades Hearts Diamonds Diamonds Diamonds Spades
15
Outline
fig18_02.cpp output (1 of 1)
16
17
18
19
Using masks
Integer value with specific bits set to 1 Used to hide some bits while selecting others
Use with AND
20
21
To create mask
Want mask of 1000000 0000 How many bits in unsigned?
sizeof(unsigned) * 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Fig. 18.5: fig18_05.cpp // Printing an unsigned integer in bits. #include <iostream> using std::cout; using std::cin; using std::endl; #include <iomanip> using std::setw; void displayBits( unsigned ); int main() { unsigned inputValue; cout << "Enter an unsigned integer: "; cin >> inputValue; displayBits( inputValue ); // prototype
22
Outline
fig18_05.cpp (1 of 2)
return 0;
} // end main
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// display bits of an unsigned integer value void displayBits( unsigned value ) { const int SHIFT = 8 * sizeof( unsigned ) - 1; const unsigned MASK = 1 << SHIFT; cout << setw( 10 ) << value << " = ";
23
Outline
for ( unsigned i = cout << ( value value <<= 1; // shift value left by 1 if ( i % 8 == 0 ) cout << ' '; } // end for cout << endl; } // end function displayBits
SHIFT = 32 - 1 = 31 MASK = 10000000 00000000 00000000 00000000 fig18_05.cpp 1; i <= SHIFT + 1; i++ ) { & MASK ? '1' : '0' ); output (1 of 1)
// output a space after 8 bits
fig18_05.cpp (2 of 2)
Shift value left by 1 to examine next bit. Note use of <<= (same as value = value << 1).
Bitwise AND value and mask. If it is nonzero (true), then the leftmost digit is a 1.
24
| (OR)
x | y
^ (Exclusive OR)
x ^ y
~ (Complement)
~x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Fig. 18.7: fig18_07.cpp // Using the bitwise AND, bitwise inclusive OR, bitwise // exclusive OR and bitwise complement operators. #include <iostream> using std::cout; using std::cin; #include <iomanip> using std::endl; using std::setw; void displayBits( unsigned ); int main() { unsigned unsigned unsigned unsigned // prototype
25
Outline
fig18_07.cpp (1 of 4)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// demonstrate bitwise & number1 = 2179876355; mask = 1; cout << "The result of combining the following\n"; displayBits( number1 ); displayBits( mask ); cout << "using the bitwise AND operator & is\n"; displayBits( number1 & mask );
26
Outline
fig18_07.cpp (2 of 4)
// demonstrate bitwise | number1 = 15; setBits = 241; cout << "\nThe result of combining the following\n"; displayBits( number1 ); displayBits( setBits ); cout << "using the bitwise inclusive OR operator | is\n"; displayBits( number1 | setBits );
// demonstrate bitwise exclusive OR number1 = 139; number2 = 199; cout << "\nThe result of combining the following\n"; displayBits( number1 ); displayBits( number2 ); cout << "using the bitwise exclusive OR operator ^ is\n"; displayBits( number1 ^ number2 );
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
27
// demonstrate bitwise complement number1 = 21845; cout << "\nThe one's complement of\n"; displayBits( number1 ); cout << "is" << endl; displayBits( ~number1 ); return 0; } // end main // display bits of an unsigned integer value void displayBits( unsigned value ) { const int SHIFT = 8 * sizeof( unsigned ) - 1; const unsigned MASK = 1 << SHIFT; cout << setw( 10 ) << value << " = "; for ( unsigned i = 1; i <= SHIFT + 1; i++ ) { cout << ( value & MASK ? '1' : '0' ); value <<= 1; // shift value left by 1 if ( i % 8 == 0 ) cout << ' '; } // end for // output a space after 8 bits
Outline
fig18_07.cpp (3 of 4)
77 78 79 80
28
cout << endl; } // end function displayBits
Outline
fig18_07.cpp (4 of 4) fig18_07.cpp output (1 of 1)
The result of combining the following 2179876355 = 10000001 11101110 01000110 00000011 1 = 00000000 00000000 00000000 00000001 using the bitwise AND operator & is 1 = 00000000 00000000 00000000 00000001 The result of combining the following 15 = 00000000 00000000 00000000 241 = 00000000 00000000 00000000 using the bitwise inclusive OR operator 255 = 00000000 00000000 00000000 The result of combining the following 139 = 00000000 00000000 00000000 199 = 00000000 00000000 00000000 using the bitwise exclusive OR operator 76 = 00000000 00000000 00000000
The one's complement of 21845 = 00000000 00000000 01010101 01010101 is 4294945450 = 11111111 11111111 10101010 10101010
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 18.11: fig18_11.cpp // Using the bitwise shift operators. #include <iostream> using std::cout; using std::cin; using std::endl; #include <iomanip> using std::setw; void displayBits( unsigned ); int main() { unsigned number1 = 960; // demonstrate bitwise left shift cout << "The result of left shifting\n"; displayBits( number1 ); cout << "8 bit positions using the left " << "shift operator is\n"; displayBits( number1 << 8 ); // prototype
29
Outline
fig18_07.cpp (1 of 3)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
// demonstrate bitwise right shift cout << "\nThe result of right shifting\n"; displayBits( number1 ); cout << "8 bit positions using the right " << "shift operator is\n"; displayBits( number1 >> 8 ); return 0;
30
Outline
fig18_07.cpp (2 of 3)
} // end main
// display bits of an unsigned integer value void displayBits( unsigned value ) { const int SHIFT = 8 * sizeof( unsigned ) - 1; const unsigned MASK = 1 << SHIFT; cout << setw( 10 ) << value << " = "; for ( unsigned i = 1; i <= SHIFT + 1; i++ ) { cout << ( value & MASK ? '1' : '0' ); value <<= 1; // shift value left by 1 if ( i % 8 == 0 ) cout << ' '; } // end for // output a space after 8 bits
53 54 55 56
31
cout << endl; } // end function displayBits
Outline
fig18_07.cpp (3 of 3) fig18_07.cpp output (1 of 1)
The result of left shifting 960 = 00000000 00000000 00000011 11000000 8 bit positions using the left shift operator is 245760 = 00000000 00000011 11000000 00000000 The result of right shifting 960 = 00000000 00000000 00000011 11000000 8 bit positions using the right shift operator is 3 = 00000000 00000000 00000000 00000011
32
33
myCard.face = 10;
face has 4 bits, can store values 0 - 15 suit can store 0 - 3 color can store 0 or 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Fig. 18.14: fig18_14.cpp // Representing cards with bit fields in a struct. #include <iostream> using std::cout; using std::endl; #include <iomanip>
34
Outline
fig18_14.cpp (1 of 3)
using std::setw;
// BitCard structure definition with bit fields struct BitCard { unsigned face : 4; // 4 bits; 0-15 unsigned suit : 2; // 2 bits; 0-3 unsigned color : 1; // 1 bit; 0-1 }; // end struct BitBard void fillDeck( BitCard * const ); void deal( const BitCard * const ); // prototype // prototype
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
35
return 0; } // end main // initialize BitCards void fillDeck( BitCard * const wDeck ) { for ( int i = 0; i <= 51; i++ ) { wDeck[ i ].face = i % 13; wDeck[ i ].suit = i / 13; wDeck[ i ].color = i / 26; } // end for } // end function fillDeck
Outline
fig18_14.cpp (2 of 3) Assign to bit fields as normal, but be careful of each field's range.
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 k1 (column 1); cards 26-51 subscripted k2 (column 2) void deal( const BitCard * const wDeck ) { for ( int k1 = 0, k2 = k1 + 26; k1 <= 25; k1++, k2++ ) { cout << "Card:" << setw( 3 ) << wDeck[ k1 ].face << " Suit:" << setw( 2 ) << wDeck[ k1 ].suit << " Color:" << setw( 2 ) << wDeck[ k1 ].color << " " << "Card:" << setw( 3 ) << wDeck[ k2 ].face << " Suit:" << setw( 2 ) << wDeck[ k2 ].suit << " Color:" << setw( 2 ) << wDeck[ k2 ].color << endl; } // end for } // end function deal
36
Outline
fig18_14.cpp (3 of 3)
Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card:
0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12
Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit:
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card:
0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12
Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit:
2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3
Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
37
Outline
fig18_14.cpp output (1 of 1)
38
Cannot take address of bit fields Use unnamed bit fields to pad structure
Struct Example unsigned a : unsigned : unsigned b : }; Struct Example unsigned a : unsigned : unsigned b : }; { 13; 3; 4;
39
40
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Fig. 18.17: fig18_17.cpp // Using functions isdigit, isalpha, isalnum and isxdigit. #include <iostream> using std::cout; using std::endl; #include <cctype> // character-handling function prototypes
41
Outline
fig18_17.cpp (1 of 2)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
cout << << << << << << << cout << << << << << << << << << << <<
"\nAccording to isalnum:\n" ( isalnum( 'A' ) ? "A is a" : "A is not a" ) " digit or a letter\n" ( isalnum( '8' ) ? "8 is a" : "8 is not a" ) " digit or a letter\n" ( isalnum( '#' ) ? "# is a" : "# is not a" ) " digit or a letter\n"; "\nAccording to isxdigit:\n" ( isxdigit( 'F' ) ? "F is a" : " hexadecimal digit\n" ( isxdigit( 'J' ) ? "J is a" : " hexadecimal digit\n" ( isxdigit( '7' ) ? "7 is a" : " hexadecimal digit\n" ( isxdigit( '$' ) ? "$ is a" : " hexadecimal digit\n" ( isxdigit( 'f' ) ? "f is a" : " hexadecimal digit" << endl;
42
Outline
fig18_17.cpp (2 of 2)
return 0;
} // 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
43
Outline
fig18_17.cpp output (1 of 1)
44
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Fig. 18.18: fig18_18.cpp // Using functions islower, isupper, tolower and toupper. #include <iostream> using std::cout; using std::endl; #include <cctype> // character-handling function prototypes
45
Outline
fig18_18.cpp (1 of 2)
int main() { cout << << << << << << << << <<
cout << << << << << << <<
"According to islower:\n" ( islower( 'p' ) ? "p is a" " lowercase letter\n" ( islower( 'P' ) ? "P is a" " lowercase letter\n" ( islower( '5' ) ? "5 is a" " lowercase letter\n" ( islower( '!' ) ? "! is a" " lowercase letter\n";
: "p is not a" ) : "P is not a" ) : "5 is not a" ) : "! is not a" )
"\nAccording to isupper:\n" ( isupper( 'D' ) ? "D is an" : "D is not an" ) " uppercase letter\n" ( isupper( 'd' ) ? "d is an" : "d is not an" ) " uppercase letter\n" ( isupper( '8' ) ? "8 is an" : "8 is not an" ) " uppercase letter\n"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<< ( isupper( '$' ) ? "$ is an" : "$ is not an" ) << " uppercase letter\n"; cout << << << << << << << << "\nu converted to static_cast< char "\n7 converted to static_cast< char "\n$ converted to static_cast< char "\nL converted to static_cast< char uppercase is " >( toupper( 'u' uppercase is " >( toupper( '7' uppercase is " >( toupper( '$' lowercase is " >( tolower( 'L'
46
Outline
fig18_18.cpp (2 of 2)
) ) ) ) ) ) ) ) << endl;
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 7 $ L converted converted converted converted to to to to uppercase uppercase uppercase lowercase is is is is U 7 $ l
47
Outline
fig18_18.cpp output (1 of 1)
48
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.
49
isgraph
Returns true if character can be displayed, not including space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 18.19: fig18_19.cpp // Using functions isspace, iscntrl, ispunct, isprint, isgraph. #include <iostream> using std::cout; using std::endl; #include <cctype> // character-handling function prototypes
50
Outline
fig18_19.cpp (1 of 2)
int main() { cout << << << << << << <<
cout << << << << <<
"According to isspace:\nNewline " ( isspace( '\n' ) ? "is a" : "is not a" ) " whitespace character\nHorizontal tab " ( isspace( '\t' ) ? "is a" : "is not a" ) " whitespace character\n" ( isspace( '%' ) ? "% is a" : "% is not a" ) " whitespace character\n"; "\nAccording to iscntrl:\nNewline " ( iscntrl( '\n' ) ? "is a" : "is not a" ) " control character\n" ( iscntrl( '$' ) ? "$ is a" : "$ is not a" ) " control character\n";
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
cout << << << << << << << cout << << << << << cout << << << << <<
"\nAccording to ispunct:\n" ( ispunct( ';' ) ? "; is a" : "; is not a" ) " punctuation character\n" ( ispunct( 'Y' ) ? "Y is a" : "Y is not a" ) " punctuation character\n" ( ispunct( '#' ) ? "# is a" : "# is not a" ) " punctuation character\n"; "\nAccording to isprint:\n" ( isprint( '$' ) ? "$ is a" : "$ is not a" ) " printing character\nAlert " ( isprint( '\a' ) ? "is a" : "is not a" ) " printing character\n"; "\nAccording to isgraph:\n" ( isgraph( 'Q' ) ? "Q is a" : "Q is not a" ) " printing character other than a space\nSpace " ( isgraph( ' ' ) ? "is a" : "is not a" ) " printing character other than a space" << endl;
51
Outline
fig18_19.cpp (2 of 2)
return 0;
} // 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
52
Outline
fig18_19.cpp output (1 of 1)
53
54
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Fig. 18.21: fig18_21.cpp // Using atof. #include <iostream> using std::cout; using std::endl; #include <cstdlib> // atof prototype
55
Outline
fig18_21.cpp (1 of 1) fig18_21.cpp output (1 of 1)
The string "99.0" converted to double is 99 The converted value divided by 2 is 49.5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Fig. 18.22: fig18_22.cpp // Using atoi. #include <iostream> using std::cout; using std::endl; #include <cstdlib> // atoi prototype
56
Outline
fig18_22.cpp (1 of 1) fig18_22.cpp output (1 of 1)
The string "2593" converted to int is 2593 The converted value minus 593 is 2000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Fig. 18.23: fig18_23.cpp // Using atol. #include <iostream> using std::cout; using std::endl; #include <cstdlib> // atol prototype
57
Outline
fig18_23.cpp (1 of 1) fig18_23.cpp output (1 of 1)
The string "1000000" converted to long int is 1000000 The converted value divided by 2 is 500000
58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Fig. 18.24: fig18_24.cpp // Using strtod. #include <iostream> using std::cout; using std::endl; #include <cstdlib> // strtod prototype
59
Outline
fig18_24.cpp (1 of 1) fig18_24.cpp output (1 of 1)
int main() { double d; const char *string1 = "51.2% are admitted"; char *stringPtr;
d = strtod( string1, &stringPtr ); cout << "The string \"" << string1 << "\" is converted to the\ndouble value " << d << " and the string \"" << stringPtr << "\"" << endl; return 0; } // end main
The string "51.2% are admitted" is converted to the double value 51.2 and the string "% are admitted"
60
long strtoul
As above, with unsigned long
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Fig. 18.25: fig18_25.cpp // Using strtol. #include <iostream> using std::cout; using std::endl; #include <cstdlib> // strtol prototype
61
Outline
fig18_25.cpp (1 of 1)
original string is "-1234567abc" converted value is -1234567 remainder of the original string is "abc" converted value plus 567 is -1234000
62
Outline
fig18_25.cpp output (1 of 1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Fig. 18.26: fig18_26.cpp // Using strtoul. #include <iostream> using std::cout; using std::endl; #include <cstdlib> // strtoul prototype
63
Outline
fig18_26.cpp (1 of 1)
int main() { unsigned long x; const char *string1 = "1234567abc"; char *remainderPtr;
x = strtoul( string1, &remainderPtr, 0 ); cout << << << << << << "The original string is \"" << string1 "\"\nThe converted value is " << x "\nThe remainder of the original string is \"" remainderPtr "\"\nThe converted value minus 567 is " x - 567 << endl;
original string is "1234567abc" converted value is 1234567 remainder of the original string is "abc" converted value minus 567 is 1234000
64
Outline
fig18_26.cpp output (1 of 1)
65
66
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Fig. 18.28: fig18_28.cpp // Using strchr. #include <iostream> using std::cout; using std::endl; #include <cstring> // strchr prototype
67
Outline
fig18_28.cpp (1 of 2)
int main() { const char *string1 = "This is a test"; char character1 = 'a'; char character2 = 'z';
if ( strchr( string1, character1 ) != NULL ) cout << '\'' << character1 << "' was found in \"" << string1 << "\".\n"; else cout << '\'' << character1 << "' was not found in \"" << string1 << "\".\n";
if ( strchr( string1, character2 ) != NULL ) cout << '\'' << character2 << "' was found in \"" << string1 << "\".\n"; else cout << '\'' << character2 << "' was not found in \"" << string1 << "\"." << endl;
29 30 31 32
68
return 0; } // end main
Outline
fig18_28.cpp (2 of 2) fig18_28.cpp output (1 of 1)
'a' was found in "This is a test". 'z' was not found in "This is a test".
69
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Fig. 18.29: fig18_29.cpp // Using strcspn. #include <iostream> using std::cout; using std::endl; #include <cstring> // strcspn prototype
70
Outline
fig18_29.cpp (1 of 1) fig18_29.cpp output (1 of 1)
int main() { const char *string1 = "The value is 3.14159"; const char *string2 = "1234567890";
cout << << << << "string1 = " << string1 << "\nstring2 = " << string2 "\n\nThe length of the initial segment of string1" "\ncontaining no characters from string2 = " strcspn( string1, string2 ) << endl;
string1 = The value is 3.14159 string2 = 1234567890 The length of the initial segment of string1 containing no characters from string2 = 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Fig. 18.30: fig18_30.cpp // Using strpbrk. #include <iostream> using std::cout; using std::endl; #include <cstring> // strpbrk prototype
71
Outline
fig18_30.cpp (1 of 1) fig18_30.cpp output (1 of 1)
int main() { const char *string1 = "This is a test"; const char *string2 = "beware";
cout << << << << "Of the characters in \"" << string2 << "\"\n'" *strpbrk( string1, string2 ) << '\'' " is the first character to appear in\n\"" string1 << '\"' << endl;
Of the characters in "beware" 'a' is the first character to appear in "This is a test"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Fig. 18.31: fig18_31.cpp // Using strrchr. #include <iostream> using std::cout; using std::endl; #include <cstring> // strrchr prototype
72
Outline
fig18_31.cpp (1 of 1) fig18_31.cpp output (1 of 1)
int main() { const char *string1 = "A zoo has many animals including zebras"; int c = 'z';
cout << << << << "The remainder of string1 beginning with the\n" "last occurrence of character '" static_cast< char >( c ) // print as char not int "' is: \"" << strrchr( string1, c ) << '\"' << endl;
return 0;
} // end main
The remainder of string1 beginning with the last occurrence of character 'z' is: "zebras"
73
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Fig. 18.32: fig18_32.cpp // Using strspn. #include <iostream> using std::cout; using std::endl; #include <cstring> // strspn prototype
74
Outline
fig18_32.cpp (1 of 1)
int main() { const char *string1 = "The value is 3.14159"; const char *string2 = "aehils Tuv";
cout << << << << << "string1 = " << string1 "\nstring2 = " << string2 "\n\nThe length of the initial segment of string1\n" "containing only characters from string2 = " strspn( string1, string2 ) << endl;
return 0;
} // end main
string1 = The value is 3.14159 string2 = aehils Tuv The length of the initial segment of string1 containing only characters from string2 = 13
75
Outline
fig18_32.cpp output (1 of 1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Fig. 18.33: fig18_33.cpp // Using strstr. #include <iostream> using std::cout; using std::endl; #include <cstring> // strstr prototype
76
Outline
fig18_33.cpp (1 of 1)
int main() { const char *string1 = "abcdefabcdef"; const char *string2 = "def";
cout << << << << "string1 = " << string1 << "\nstring2 = " << string2 "\n\nThe remainder of string1 beginning with the\n" "first occurrence of string2 is: " strstr( string1, string2 ) << endl;
string1 = abcdefabcdef string2 = def The remainder of string1 beginning with the first occurrence of string2 is: defabcdef
77
Outline
fig18_33.cpp output (1 of 1)
78
79
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Fig. 18.35: fig18_35.cpp // Using memcpy. #include <iostream> using std::cout; using std::endl; #include <cstring> // memcpy prototype
80
Outline
fig18_35.cpp (1 of 1) fig18_35.cpp output (1 of 1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Fig. 18.36: fig18_36.cpp // Using memmove. #include <iostream> using std::cout; using std::endl; #include <cstring> // memmove prototype
81
Outline
fig18_36.cpp (1 of 1) fig18_36.cpp output (1 of 1)
The string in array x before memmove is: Home Sweet Home The string in array x after memmove is: Sweet Home Home
82
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Fig. 18.37: fig18_37.cpp // Using memcmp. #include <iostream> using std::cout; using std::endl; #include <iomanip>
83
Outline
fig18_37.cpp (1 of 1)
using std::setw;
#include <cstring> // memcmp prototype
int main() { char s1[] = "ABCDEFG"; char s2[] = "ABCDXYZ"; cout << << << << << << "s1 = " << s1 << "\ns2 = " << s2 << endl "\nmemcmp(s1, s2, 4) = " << setw( 3 ) memcmp( s1, s2, 4 ) << "\nmemcmp(s1, s2, 7) = " setw( 3 ) << memcmp( s1, s2, 7 ) "\nmemcmp(s2, s1, 7) = " << setw( 3 ) memcmp( s2, s1, 7 ) << endl;
84
Outline
fig18_37.cpp output (1 of 1)
85
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Fig. 18.38: fig18_38.cpp // Using memchr. #include <iostream> using std::cout; using std::endl; #include <cstring> // memchr prototype
86
Outline
fig18_38.cpp (1 of 1) fig18_38.cpp output (1 of 1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Fig. 18.39: fig18_39.cpp // Using memset. #include <iostream> using std::cout; using std::endl; #include <cstring> // memset prototype
87
Outline
fig18_39.cpp (1 of 1) fig18_39.cpp output (1 of 1)