
Starting Out with C++ from Control Structures to Objects Eighth Edition by Tony Gaddis
January 10, 2019
0 Komen
![]() |
Starting Out with C++ from Control Structures to Objects Eighth Edition by Tony Gaddis |
Download Starting Out with C++ from Control Structures to Objects Eighth Edition by Tony Gaddis easily in PDF format for free.
The string class is an abstract data
type. This means it is not a built-in, primitive data type like int or
char. Instead, it is a programmer-defined data type that accompanies the
C++ language. It provides many capabilities that make storing and
working with strings easy and intuitive. Chapter 10 Characters,
C-Strings, and More About the string Class In the function countChars,
strPtr points to the C-string that is to be searched and ch contains the
character to look for.The first operand is less than the second operand
if the mismatched character in the first operand is less than its
counterpart in the second operand. Likewise, the first operand is
greater than the second operand if the mismatched character in the first
operand is greater than its counterpart in the second operand.
The
while loop in lines 41 through 46 repeats as long as the character that
strPtr points to is not the null terminator: while (*strPtr != '\0')
Inside the loop, the if statement in line 43 compares the character that
strPtr points to with the character in ch: if (*strPtr == ch) If the
two are equal, the variable times is incremented in line 44. ( times
keeps a running total of the number of times the character appears.) The
last statement in the loop is strPtr++; This statement increments the
address in strPtr.
This causes strPtr to point to the
next character in the string. Then, the loop starts over. When strPtr
finally reaches the null terminator, the loop terminates, and the
function returns the value in times. For another example, see the String
Manipulation Case Study, available for download from the book’s
companion Web site at www.pearsonhighered.com/gaddis . The counter is
updated in line 25 and then the user is asked for the next number. When
all the numbers are entered, the user terminates the loop by entering ‘
Q’ or ‘ q’. If one or more numbers are entered, their average is
displayed.
The
string-to-numeric conversion functions can also help with a common input
problem. Recall from Chapter 3that using cin >> and then calling
cin.get causes problems because the >> operator leaves the newline
character in the keyboard buffer. When the cin.get function executes,
the first character it sees in the keyboard buffer is the newline
character, so it reads no further. The same problem exists when a
program uses cin >> and then calls cin.getline to read a line of
input. For example, look at the following code.
(Assume idNumber is an int and name is
a char array.) There is a great difference between a number that is
stored as a string and one stored as a numeric value. The string “
26792” isn’t actually a number, but a series of ASCII codes representing
the individual digits of the number. It uses six bytes of memory
(including the null terminator). Because it isn’t an actual number, it’s
not possible to perform mathematical operations with it, unless it is
first converted to a numeric value. Several functions exist in the C++
library for converting C-string representations of numbers into numeric
values.
Table 10-4shows some
of these. Note that all of these functions require the cstdlib header
file. Relational operators perform comparisons on string objects in a
fashion similar to the way the strcmp function compares C-strings. One
by one, each character in the first operand is compared with the
character in the corresponding position in the second operand. If all
the characters in both strings match, the two strings are equal. Other
relationships can be determined if two characters in corresponding
positions do not match.
Get Copy From Your Book Here
Any question or need any help, please feel free to contact us.
Any question or need any help, please feel free to contact us.
Get Copy From Your Book Here
Any question or need any help, please feel free to contact us.
Any question or need any help, please feel free to contact us.
0 Response to "Starting Out with C++ from Control Structures to Objects Eighth Edition by Tony Gaddis"
Post a Comment