Title: Pointers and Strings
1Pointers and Strings
- One of most powerful features of the C
programming - Usage
- Passing by address using pointers
- Dynamic allocation using pointers
- Exchangeable between array and pointer notation
- Used in object-oriented programming
2Pointers and Strings
- Pointer is a special data variable, which is
introduced to hold memory address of other
variables. - Pointer also has four properties or attributes
name, data type, value, and location in memory. - Value of pointer is the address of other variable
where the pointer directs or points to.
3Pointers and Strings
- The variable name of any data type can be used to
directly reference a value, such as - Pointer can be used to indirectly reference the
value, which is called indirection.
int count7 coutltlt8count
count references value 7 and it
pint out 15
4Pointers and Strings
declare a regular int variable
int countPtr, count declare an
int pointer variable
count7 countPtrcount
Memory address of variable count
int pointer points or directs to an int variable
5Pointers and Strings
double xPtr, yPtr declare two
double pointer variables
6Pointers and Strings
count
7
countPtrcount
address
countPtr
coutltlt8count coutltlt8 countPtr countPtr9 co
utltlt8count
apply to a point to indirectly reference the
value of a variable to which the pointer points
is to be called dereference
Print out 17
7// Fig. 5.4 fig05_04.cpp // Using the and
operators include ltiostreamgt using
stdcout using stdendl int main() int
a // a is an integer int aPtr //
aPtr is a pointer to an integer a 7
aPtr a // aPtr set to address of a
cout ltlt "The address of a is " ltlt a ltlt
"\nThe value of aPtr is " ltlt aPtr
8 cout ltlt "\n\nThe value of a is " ltlt a
ltlt "\nThe value of aPtr is " ltlt aPtr cout
ltlt "\n\nShowing that and are inverses of "
ltlt "each other.\naPtr " ltlt aPtr
ltlt "\naPtr " ltlt aPtr ltlt endl return
0
9The address of a is 0x7bfe07c8 The value of aPtr
is 0x7bfe07c8 The value of a is 7 The value of
aPtr is 7 Showing that and are inverses of
each other. aPtr 0x7bfe07c8 aPtr
0x7bfe07c8
10Pointers and Strings
The third way to pass information from calling
function to called function, review
call-by-value
11// Fig. 5.6 fig05_06.cpp // Cube a variable
using call-by-value include ltiostreamgt using
stdcout using stdendl int cubeByValue( int
) // prototype int main() int number
5
12 cout ltlt "The original value of number is " ltlt
number number cubeByValue( number )
cout ltlt "\nThe new value of number is " ltlt number
ltlt endl return 0 int cubeByValue( int n
) return n n n // cube local variable
n
The original value of number is 5 The new value
of number is 125
13Pointers and Strings
The third way to pass information from calling
function to called function, review
call-by-reference
14// Fig. 5.7 fig05_07.cpp // Cube a variable
using call-by-reference // with a pointer
argument include ltiostreamgt using
stdcout using stdendl void
cubeByReference( int ) // prototype int
main() int number 5
15 cout ltlt "The original value of number is " ltlt
number cubeByReference( number ) cout ltlt
"\nThe new value of number is " ltlt number ltlt
endl return 0 void cubeByReference( int
nPtr ) nPtr nPtr nPtr nPtr //
cube number in main
The original value of number is 5 The new value
of number is 125
16Pointers and Strings
- Important issue
- Call-by-value can only pass a value to a called
function. The called function can not change the
value of arguments - Called-by-reference can pass information to a
called function. Every little modification or
change of reference variable in the called
function can effects original variable in calling
function, since the reference variable is like
alias to the original variable.
17Pointers and Strings
- Important issue
- Call-by-address (pointer) can pass information to
called function. It actually passes the address
and through the address to access the original
variable by using pointer's dereference. Better,
but still not secure. - How can we protect original data after calling?
- How can we lock the pointer direction?
18Pointers and Strings
- const qualifier can be used to accomplish the
tasks. - Review
- non-constant pointer pointer can change
direction - non-constant data the data value of a variable
can be changed
const float PI3.1415926
constant data. The value of PI can not be changed
!
19Pointers and Strings
- There are four ways to pass pointer to a function
- a non-constant pointer to non-constant data
- a non-constant pointer to constant data
- a constant pointer to non-constant data
- a constant pointer to constant data
20Pointers and Strings
- a non-constant pointer to non-constant data (data
can be modified through the pointer's
dereference, the pointer can modified to point
other data variable it do not need to include
const) - Example
21// Fig. 5.10 fig05_10.cpp // Converting
lowercase letters to uppercase letters // using a
non-constant pointer to non-constant
data include ltiostreamgt using stdcout using
stdendl include ltcctypegt void
convertToUppercase( char ) int main()
char string "characters and 32.98"
22 cout ltlt "The string before conversion is " ltlt
string convertToUppercase( string ) cout
ltlt "\nThe string after conversion is "
ltlt string ltlt endl return 0 void
convertToUppercase( char sPtr ) while (
sPtr ! '\0' ) if ( islower( sPtr ) )
sPtr toupper( sPtr ) // convert to
uppercase sPtr // move sPtr to the
next character
23The string before conversion is characters and
32.98 The string after conversion is
CHARACTERS AND 32.98
24Pointers and Strings
- a non-constant pointer to constant data (data can
not be modified through the pointer's
dereference, but the pointer can be modified to
point other data variable it do not need to
include const) - Example
25// Fig. 5.11 fig05_11.cpp // Printing a string
one character at a time using // a non-constant
pointer to constant data include
ltiostreamgt using stdcout using
stdendl void printCharacters( const char
) int main() char string "print
characters of a string" cout ltlt "The string
is\n"
26 printCharacters( string ) cout ltlt endl
return 0 // In printCharacters, sPtr cannot
modify the character // to which it points. sPtr
is a "read-only" pointer void printCharacters(
const char sPtr ) for ( sPtr ! '\0'
sPtr ) // no initialization cout ltlt
sPtr
27The string is print characters of a string
28Pointers and Strings
int a7 const int aPtr kind of
read-only pointer aPtra int pointer
points to address of int a
and the value of a can be used,
but can not be
changed. coutltltaPtr aPtr8 // can not do
that!
29Pointers and Strings
Example to show that you can not use non-constant
pointer to Change the value of constant data.
30// Fig. 5.12 fig05_12.cpp // Attempting to
modify data through a // non-constant pointer to
constant data. include ltiostreamgt void f( const
int ) int main() int y f( y )
// f attempts illegal modification return 0
31 // xPtr cannot modify the value of the variable
// to which it points void f( const int xPtr
) xPtr 100 // cannot modify a const
object
32Pointers and Strings
- constant pointer to non-constant data is a
pointer that always points to the same memory
location, and the data at that location can be
modified through the pointer. - That is the default for an array name
33// Fig. 5.14 fig05_14.cpp // Attempting to
modify a constant pointer to // constant
data. include ltiostreamgt using stdcout using
stdendl int main() int x 5, y
34 const int const ptr x // ptr is a constant
pointer to a //
constant integer. ptr always
// points to the same location
// and the integer at that
// location cannot
be modified. cout ltlt ptr ltlt endl ptr
7 ptr y return 0
Fig05_14.cpp In function int main()' Fig05_14.c
pp19 assignment of read-only location Fig05_14.c
pp20 assignment of read-only variable ptr'
35Pointers and Strings
- Constant pointer points to same memory location.
The data can - Be modified. But the pointer only points variable
- when it must be declared and initialized
together. - Constant pointer is equivalent to array name.
int a int const aPtra int
customerID200 int const customerIDPtrcustome
rID //or customerID0
36Constant pointer points to constant data
// Fig. 5.14 fig05_14.cpp // Attempting to
modify a constant pointer to constant
data. include ltiostream.hgt int main() int x
5, y const int const ptr x // ptr is a
constant pointer to a // constant integer. ptr
always points to the same location // and the
integer at that location cannot be modified.
cout ltlt ptr ltlt endl ptr 7 ptr y
return 0
37Pointers and Strings
Summary Non-constant pointer points to
non-constant data variable int a
int aPtr aPtra Non-constant pointer points
to constant data variable int a
const int aPtr aPtra Constant pointer
points to non-constant data variable int a
int const aPtra
38Pointers and Strings
Summary Constant pointer points to constant
data variable int a const int
const aPtra
39Pointers and Strings
- Example Bubble sort using call-by-address with a
pointer
40// Fig. 5.15 fig05_15.cpp // This program puts
values into an array, sorts the values into //
ascending order, and prints the resulting
array. include ltiostreamgt using
stdcout using stdendl include
ltiomanipgt using stdsetw void bubbleSort( int
, const int )
41int main() const int arraySize 10 int
a arraySize 2, 6, 4, 8, 10, 12, 89, 68,
45, 37 int i cout ltlt "Data items in
original order\n" for ( i 0 i lt
arraySize i ) cout ltlt setw( 4 ) ltlt a i
bubbleSort( a, arraySize ) //
sort the array cout ltlt "\nData items in
ascending order\n" for ( i 0 i lt
arraySize i ) cout ltlt setw( 4 ) ltlt a i
42 cout ltlt endl return 0 void
bubbleSort( int array, const int size )
void swap( int const, int const ) for (
int pass 0 pass lt size - 1 pass )
for ( int j 0 j lt size - 1 j ) if
( array j gt array j 1 ) swap(
array j , array j 1 )
43void swap( int const element1Ptr, int const
element2Ptr ) int hold element1Ptr
element1Ptr element2Ptr element2Ptr
hold
Data items in original order 2 6 4 8 10
12 89 68 45 37 Data items in ascending
order 2 4 6 8 10 12 37 45 68 89
44- Determine the data type size or array size
(bytes) using sizeof()
45// Fig. 5.16 fig05_16.cpp // Sizeof operator
when used on an array name // returns the number
of bytes in the array. include ltiostreamgt using
stdcout using stdendl size_t getSize(
double ) int main() double array 20
46 cout ltlt "The number of bytes in the array is "
ltlt sizeof( array ) ltlt "\nThe
number of bytes returned by getSize is "
ltlt getSize( array ) ltlt endl return
0 size_t getSize( double ptr ) return
sizeof( ptr )
The number of bytes in the array is 160 The
number of bytes returned by getSize is 4
47// Fig. 5.17 fig05_17.cpp // Demonstrating the
sizeof operator include ltiostreamgt using
stdcout using stdendl include
ltiomanipgt int main() char c short s
int i
48 long l float f double d long double
ld int array 20 , ptr array cout ltlt
"sizeof c " ltlt sizeof c ltlt
"\tsizeof(char) " ltlt sizeof( char ) ltlt
"\nsizeof s " ltlt sizeof s ltlt
"\tsizeof(short) " ltlt sizeof( short )
ltlt "\nsizeof i " ltlt sizeof i ltlt
"\tsizeof(int) " ltlt sizeof( int ) ltlt
"\nsizeof l " ltlt sizeof l ltlt
"\tsizeof(long) " ltlt sizeof( long ) ltlt
"\nsizeof f " ltlt sizeof f
49 ltlt "\tsizeof(float) " ltlt sizeof( float )
ltlt "\nsizeof d " ltlt sizeof d ltlt
"\tsizeof(double) " ltlt sizeof( double )
ltlt "\nsizeof ld " ltlt sizeof ld ltlt
"\tsizeof(long double) " ltlt sizeof( long double
) ltlt "\nsizeof array " ltlt sizeof array
ltlt "\nsizeof ptr " ltlt sizeof ptr
ltlt endl return 0
50sizeof c 1 sizeof(char) 1 sizeof s 2
sizeof(short) 2 sizeof i 4 sizeof(int)
4 sizeof l 4 sizeof(long) 4 sizeof f 4
sizeof(float) 4 sizeof d 8 sizeof(double)
8 sizeof ld 8 sizeof(long double)
8 sizeof array 80 sizeof ptr 4
51Pointers and Strings
- Pointer expression and pointer arithmetic
- Pointer can be incremented (), decremented
(--), an integer adding and subtracting
int a int array10 int ptra ptr1 ptrarr
ay ptrptr4
Points location after sizeof(int) 42334324 (int
size)
Points to the first element of array,
array0 Points to the fifth element of array,
array4
52Pointers and Strings
- Relation between pointer and array
- Constant pointer
Array name
int b20, bptr bptrb bptrb0 bpr
(bptr3) bpr3 b3 (b3)
b0
b3
Pointer using pointer notation
Pointer using array notation
b3
Array using array notation
b3
Array using pointer notation
b3
53// Fig. 5.20 fig05_20.cpp // Using subscripting
and pointer notations with arrays include
ltiostreamgt using stdcout using
stdendl int main() int b 10, 20,
30, 40 , i, offset int bPtr b // set
bPtr to point to array b cout ltlt "Array b
printed with\n" ltlt "Array subscript
notation\n"
54 for ( i 0 i lt 4 i ) cout ltlt "b" ltlt
i ltlt " " ltlt b i ltlt '\n' cout ltlt
"\nPointer/offset notation where\n" ltlt
"the pointer is the array name\n" for (
offset 0 offset lt 4 offset ) cout ltlt
"(b " ltlt offset ltlt ") " ltlt ( b
offset ) ltlt '\n' cout ltlt "\nPointer
subscript notation\n" for ( i 0 i lt 4
i ) cout ltlt "bPtr" ltlt i ltlt " " ltlt
bPtr i ltlt '\n'
55 cout ltlt "\nPointer/offset notation\n" for (
offset 0 offset lt 4 offset ) cout ltlt
"(bPtr " ltlt offset ltlt ") " ltlt (
bPtr offset ) ltlt '\n' return 0
56Array b printed with Array subscript
notation b0 10 b1 20 b2 30 b3
40 Pointer/offset notation where the pointer is
the array name (b 0) 10 (b 1) 20 (b
2) 30 (b 3) 40
57Pointer subscript notation bPtr0 10 bPtr1
20 bPtr2 30 bPtr3 40 Pointer/offset
notation (bPtr 0) 10 (bPtr 1) 20 (bPtr
2) 30 (bPtr 3) 40
58Copy String using different methods
// Fig. 5.21 fig05_21.cpp // Copying a string
using array notation // and pointer
notation. include ltiostreamgt using
stdcout using stdendl void copy1( char ,
const char ) void copy2( char , const char
)
59int main() char string1 10 , string2
"Hello", string3 10 , string4 "Good
Bye" copy1( string1, string2 ) cout ltlt
"string1 " ltlt string1 ltlt endl copy2(
string3, string4 ) cout ltlt "string3 " ltlt
string3 ltlt endl return 0
60// copy s2 to s1 using array notation void copy1(
char s1, const char s2 ) for ( int i 0
( s1 i s2 i ) ! '\0' i ) //
do nothing in body // copy s2 to s1 using
pointer notation void copy2( char s1, const char
s2 ) for ( ( s1 s2 ) ! '\0' s1,
s2 ) // do nothing in body
string1 Hello string3 Good Bye
61Pointers and Strings
- Arrays of pointers
- Each element of array of pointer is a pointer
- Each element can point different array
- Array of char pointers, specially useful in
pointing bunch of strings
char suit4"Hearts", "Diamonds", "Clubs",
"Spades" suit0"Hearts" suit1"Diamonds" s
uit2"Clubs" suit3"Spades"
62Pointers and Strings
- Example Card shuffling and dealing
63// Fig. 5.24 fig05_24.cpp // Card shuffling
dealing program include ltiostreamgt using
stdcout include ltiomanipgt using
stdios using stdsetw using
stdsetiosflags include ltcstdlibgt include
ltctimegt
64void shuffle( int 13 ) void deal( const
int 13 , const char , const char
) int main() const char suit 4
"Hearts", "Diamonds", "Clubs", "Spades"
const char face 13 "Ace", "Deuce",
"Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen",
"King" int deck 4 13 0
srand( time( 0 ) ) shuffle( deck ) deal(
deck, face, suit )
65 return 0 void shuffle( int wDeck 13
) int row, column for ( int card 1
card lt 52 card ) do row
rand() 4 column rand() 13
while( wDeck row column ! 0 )
wDeck row column card
66 void deal( const int wDeck 13 , const char
wFace, const char wSuit )
for ( int card 1 card lt 52 card )
for ( int row 0 row lt 3 row )
for ( int column 0 column lt 12 column )
if ( wDeck row column card )
cout ltlt setw( 5 ) ltlt setiosflags(
iosright ) ltlt wFace column
ltlt " of " ltlt setw( 8 ) ltlt
setiosflags( iosleft ) ltlt
wSuit row ltlt ( card 2
0 ? '\n' '\t' )
67Seven of Clubs Ace of Clubs Nine
of Clubs Ace of Diamonds Seven of
Hearts Five of Clubs Six of Spades
Queen of Hearts Ace of Hearts
Three of Hearts Three of Clubs Ten
of Clubs Three of Diamonds Jack of
Hearts Eight of Hearts Jack of Spades
Ten of Diamonds Deuce of Diamonds Nine
of Hearts King of Spades Jack of
Diamonds Seven of Spades Deuce of Clubs
Six of Clubs King of Diamonds
Four of Spades Deuce of Spades Eight
of Spades Eight of Clubs Ace of
Spades Nine of Diamonds Queen of Clubs
68Jack of Clubs Four of Diamonds Three
of Spades Nine of Spades Deuce of
Hearts Five of Spades Ten of Hearts
Queen of Spades Seven of Diamonds
Five of Hearts Queen of Diamonds Six
of Diamonds King of Hearts Four of
Hearts Ten of Spades Six of Hearts
Five of Diamonds King of Clubs Four
of Clubs Eight of Diamonds
69Pointers and Strings
- Function pointers
- A pointer to a function contains the address of
the function in memory - Array name is the address of the first element.
- Function name is the starting address of the
code. - Pointers to functions can be passed to called
function through argument return from function,
stored in array and assigned to other function
pointers. - Example (ascending and descending selection)
70// Fig. 5.26 fig05_26.cpp // Multipurpose
sorting program using function pointers include
ltiostreamgt using stdcout using
stdcin using stdendl include
ltiomanipgt using stdsetw void bubble( int ,
const int, bool ()( int, int ) ) bool
ascending( int, int ) bool descending( int, int
)
71int main() const int arraySize 10 int
order, counter, a arraySize
2, 6, 4, 8, 10, 12, 89, 68, 45, 37 cout ltlt
"Enter 1 to sort in ascending order,\n"
ltlt "Enter 2 to sort in descending order "
cin gtgt order cout ltlt "\nData items in
original order\n" for ( counter 0
counter lt arraySize counter ) cout ltlt
setw( 4 ) ltlt a counter
72 if ( order 1 ) bubble( a, arraySize,
ascending ) cout ltlt "\nData items in
ascending order\n" else bubble(
a, arraySize, descending ) cout ltlt "\nData
items in descending order\n" for (
counter 0 counter lt arraySize counter )
cout ltlt setw( 4 ) ltlt a counter cout ltlt
endl return 0
73void bubble( int work, const int size,
bool (compare)( int, int ) ) void
swap( int const, int const ) //
prototype for ( int pass 1 pass lt size
pass ) for ( int count 0 count lt size
- 1 count ) if ( (compare)( work
count , work count 1 ) ) swap(
work count , work count 1 )
74void swap( int const element1Ptr, int const
element2Ptr ) int temp temp
element1Ptr element1Ptr element2Ptr
element2Ptr temp bool ascending( int a,
int b ) return b lt a // swap if b is less
than a bool descending( int a, int b )
return b gt a // swap if b is greater than a
75Enter 1 to sort in ascending order, Enter 2 to
sort in descending order 1 Data items in
original order 2 6 4 8 10 12 89 68
45 37 Data items in ascending order 2 4 6
8 10 12 37 45 68 89 Enter 1 to sort in
ascending order, Enter 2 to sort in descending
order 2 Data items in original order 2 6
4 8 10 12 89 68 45 37 Data items in
descending order 89 68 45 37 12 10 8 6
4 2
76Pointers and Strings
- Function pointers are often used in menu driven
system - Select different function based on user input
- example
77// Fig. 5.28 fig05_28.cpp // Demonstrating an
array of pointers to functions include
ltiostreamgt using stdcout using
stdcin using stdendl void function1( int
) void function2( int ) void function3( int
) int main() void (f 3 )( int )
function1, function2, function3 int choice
78 cout ltlt "Enter a number between 0 and 2, 3 to
end " cin gtgt choice while ( choice gt 0
choice lt 3 ) (f choice )( choice
) cout ltlt "Enter a number between 0 and 2,
3 to end " cin gtgt choice cout
ltlt "Program execution completed." ltlt endl
return 0 void function1( int a ) cout ltlt
"You entered " ltlt a ltlt " so function1
was called\n\n"
79 void function2( int b ) cout ltlt "You
entered " ltlt b ltlt " so function2 was
called\n\n" void function3( int c ) cout
ltlt "You entered " ltlt c ltlt " so function3
was called\n\n"
80Enter a number between 0 and 2, 3 to end 0 You
entered 0 so function1 was called Enter a number
between 0 and 2, 3 to end 2 You entered 2 so
function3 was called Enter a number between 0
and 2, 3 to end 3 Program execution completed.
81Pointers and Strings
- C characters, string, and standard string
library functions - String literals can be stored in a char array
String literals "Jun Ni" "832 Johnson
St." "(319) 334-5432"
char color "blue" char color
'b','l','u','e','\0' char colorPtr"blue"
Null character \0
82Pointers and Strings
- You can not assign a literal string directly to a
char array, example - You can input from keyboard
char name30 name"Jun Ni"
//wrong
cingtgtname //maximum up to 29 characters, but
one word cingtgtsetw(30)gtgtname //ensure not to
exceed
83Pointers and Strings
includeltiostream.hgt main() char name30
coutltlt"Enter your fall name" cingtgtname
coutltltnameltltendl return 0
Result Enter your fall nameJun Ni
Jun
84Pointers and Strings
- Alternatively, we can use cin.getline() function
- Example
char name30 address40 cin.getline(name,30,'\n
')
85Pointers and Strings
includeltiostream.hgt main() char name30,
address40 coutltlt"Please enter your name"
cin.getline(name,30,'\n')
coutltltnameltltendl coutltlt"Please enter your
address" cin.getline(address,40,'\n')
coutltltaddressltltendl return 0
86Pointers and Strings
Result Please enter your nameJun Ni Jun
Ni Please enter your address310 Devenport
St. 310 Devenport St.
87Pointers and Strings
- String manipulation functions
- Fig 5.29 (on Page 346)
88Pointers and Strings
- Three examples strcpy, strcat, strcpm, strtok,
and strlen
89// Fig. 5.30 fig05_30.cpp // Using strcpy and
strncpy include ltiostreamgt using
stdcout using stdendl include
ltcstringgt int main() char x "Happy
Birthday to You" char y 25 , z 15
90cout ltlt "The string in array x is " ltlt x
ltlt "\nThe string in array y is " ltlt strcpy( y, x
) ltlt '\n' strncpy( z, x, 14 ) //
does not copy null character z 14 '\0'
cout ltlt "The string in array z is " ltlt z ltlt
endl return 0
91The string in array x is Happy Birthday to
You The string in array y is Happy Birthday to
You The string in array z is Happy Birthday
92// Fig. 5.31 fig05_31.cpp // Using strcat and
strncat include ltiostreamgt using
stdcout using stdendl include
ltcstringgt int main() char s1 20 "Happy
" char s2 "New Year " char s3 40
""
93 cout ltlt "s1 " ltlt s1 ltlt "\ns2 " ltlt s2
cout ltlt "\nstrcat(s1, s2) " ltlt strcat( s1, s2
) cout ltlt "\nstrncat(s3, s1, 6) " ltlt
strncat( s3, s1, 6 ) cout ltlt "\nstrcat(s3,
s1) " ltlt strcat( s3, s1 ) ltlt endl return
0
94s1 Happy s2 New Year strcat(s1, s2) Happy
New Year strncat(s3, s1, 6) Happy strcat(s3,
s1) Happy Happy New Year
95// Fig. 5.32 fig05_32.cpp // Using strcmp and
strncmp include ltiostreamgt using
stdcout using stdendl include
ltiomanipgt using stdsetw include ltcstringgt
96int main() char s1 "Happy New Year"
char s2 "Happy New Year" char s3 "Happy
Holidays" cout ltlt "s1 " ltlt s1 ltlt "\ns2
" ltlt s2 ltlt "\ns3 " ltlt s3 ltlt
"\n\nstrcmp(s1, s2) " ltlt setw( 2 ) ltlt
strcmp( s1, s2 ) ltlt "\nstrcmp(s1, s3)
" ltlt setw( 2 ) ltlt strcmp( s1, s3 ) ltlt
"\nstrcmp(s3, s1) " ltlt setw( 2 ) ltlt
strcmp( s3, s1 )
97 cout ltlt "\n\nstrncmp(s1, s3, 6) " ltlt setw( 2 )
ltlt strncmp( s1, s3, 6 ) ltlt
"\nstrncmp(s1, s3, 7) " ltlt setw( 2 )
ltlt strncmp( s1, s3, 7 ) ltlt
"\nstrncmp(s3, s1, 7) " ltlt setw( 2 )
ltlt strncmp( s3, s1, 7 ) ltlt endl return 0
98s1 Happy New Year s2 Happy New Year s3
Happy Holidays strcmp(s1, s2) 0 strcmp(s1,
s3) 1 strcmp(s3, s1) -1 strncmp(s1, s3, 6)
0 strncmp(s1, s3, 7) 6 strncmp(s3, s1, 7)
-6
99// Fig. 5.33 fig05_33.cpp // Using
strtok include ltiostreamgt using
stdcout using stdendl include
ltcstringgt int main() char string "This
is a sentence with 7 tokens" char tokenPtr
100 cout ltlt "The string to be tokenized is\n" ltlt
string ltlt "\n\nThe tokens are\n"
tokenPtr strtok( string, " " ) while (
tokenPtr ! NULL ) cout ltlt tokenPtr ltlt
'\n' tokenPtr strtok( NULL, " " )
return 0
101The string to be tokenized is This is a sentence
with 7 tokens The tokens are This is a sentence
with 7 tokens
102// Fig. 5.34 fig05_34.cpp // Using
strlen include ltiostreamgt using
stdcout using stdendl include
ltcstringgt int main() char string1
"abcdefghijklmnopqrstuvwxyz" char string2
"four" char string3 "Boston"
103 cout ltlt "The length of \"" ltlt string1 ltlt
"\" is " ltlt strlen( string1 ) ltlt "\nThe
length of \"" ltlt string2 ltlt "\" is " ltlt
strlen( string2 ) ltlt "\nThe length of \""
ltlt string3 ltlt "\" is " ltlt strlen( string3
) ltlt endl return 0
104The length of "abcdefghijklmnopqrstuvwxyz" is
26 The length of "four" is 4 The length of
"Boston" is 6