Sow C++ Cso Chapter 10 10e
Sow C++ Cso Chapter 10 10e
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
10.1
Character Testing
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Character Testing
• Requires cctype header file
FUNCTION MEANING
isalpha true if arg. is a letter, false otherwise
isalnum true if arg. is a letter or digit, false otherwise
isdigit true if arg. is a digit 0-9, false otherwise
islower true if arg. is lowercase letter, false otherwise
isprint true if arg. is a printable character, false otherwise
ispunct true if arg. is a punctuation character, false otherwise
isupper true if arg. is an uppercase letter, false otherwise
isspace true if arg. is a whitespace character, false otherwise
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 10-1
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
10.2
Character Case Conversion
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Character Case Conversion
• Require cctype header file
• Functions:
toupper: if char argument is lowercase letter, return
uppercase equivalent; otherwise, return input
unchanged
char ch1 = 'H';
char ch2 = 'e';
char ch3 = '!';
cout << toupper(ch1); // displays 'H'
cout << toupper(ch2); // displays 'E'
cout << toupper(ch3); // displays '!'
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Character Case Conversion
• Functions:
tolower: if char argument is uppercase letter, return
lowercase equivalent; otherwise, return input
unchanged
char ch1 = 'H';
char ch2 = 'e';
char ch3 = '!';
cout << tolower(ch1); // displays 'h'
cout << tolower(ch2); // displays 'e'
cout << tolower(ch3); // displays '!'
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
10.3
C-Strings
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
C-Strings
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
C-Strings
• Array of chars can be used to define storage for
string:
const int SIZE = 20;
char city[SIZE];
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using C-Strings in Program 10-5
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using C-Strings in Program 10-5
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
10.4
Library Functions for Working with
C-Strings
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Library Functions for Working
with C-Strings
• Require the cstring header file
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Library Functions for
Working with C-Strings
Functions:
• strlen(str): returns length of C-string str
char city[SIZE] = "Missoula";
cout << strlen(city); // prints 8
• strcat(str1, str2): appends str2 to the
end of str1
char location[SIZE] = "Missoula, ";
char state[3] = "MT";
strcat(location, state);
// location now has "Missoula, MT"
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Library Functions for
Working with C-Strings
Functions:
• strcpy(str1, str2): copies str2 to str1
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
10.5
C-String/Numeric Conversion
Functions
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
C-String/Numeric Conversion
Functions
• Requires <cstdlib> header file
FUNCTION PARAMETER ACTION
atoi C-string converts C-string to an int value, returns
the value
atol C-string converts C-string to a long value, returns
the value
atof C-string converts C-string to a double value,
returns the value
itoa int,C-string, converts 1st int parameter to a C-string,
int stores it in 2nd parameter. 3rd parameter is
base of converted value
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
C-String/Numeric Conversion
Functions
int iNum;
long lNum;
double dNum;
char intChar[10];
iNum = atoi("1234"); // puts 1234 in iNum
lNum = atol("5678"); // puts 5678 in lNum
dNum = atof("35.7"); // puts 35.7 in dNum
itoa(iNum, intChar, 8); // puts the string
// "2322" (base 8 for 123410) in
intChar
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
C-String/Numeric Conversion
Functions - Notes
• if C-string contains non-digits, results are
undefined
• function may return result up to non-digit
• function may return 0
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
string to Number Conversion
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The to_string Function
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
10.6
Writing Your Own C-String
Handling Functions
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Writing Your Own C-String
Handling Functions
• Designing C-String Handling Functions
• can pass arrays or pointers to char arrays
• Can perform bounds checking to ensure
enough space for results
• Can anticipate unexpected user input
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 10-12
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 10-13
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
10.7
More About the C++ string
Class
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The C++ string Class
• Special data type supports working with strings
• #include <string>
• Can define string variables in programs:
string firstName, lastName;
• Can receive values with assignment operator:
firstName = "George";
lastName = "Washington";
• Can be displayed via cout
cout << firstName << " " << lastName;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using the string class in Program 10-15
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Input into a string Object
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using cin and string objects in program 10-16
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Input into a string Object
• Use getline function to put a line of
input, possibly including spaces, into a
string:
string address;
cout << "Enter your address: ";
getline(cin,address);
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
string Comparison
• Can use relational operators directly to compare string
objects:
string str1 = "George",
str2 = "Georgia";
if (str1 < str2)
cout << str1 << " is less than "
<< str2;
• Comparison is performed similar to strcmp function.
Result is true or false
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Continued…
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Other Definitions of C++ strings
Definition Meaning
string name; defines an empty string object
string myname("Chris"); defines a string and initializes it
string yourname(myname); defines a string and initializes it
string aname(myname, 3); defines a string and initializes it with first 3
characters of myname
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using auto To Define a string Object
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using auto To Define a string Object
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
string Operators
OPERATOR MEANING
>> extracts characters from stream up to whitespace, insert
into string
<< inserts string into stream
= assigns string on right to string object on left
+= appends string on right to end of contents on left
+ concatenates two strings
[] references character in string using array notation
>, >=, <, relational operators for string comparison. Return true or
<=, ==, != false
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
string Operators
string word1, phrase;
string word2 = " Dog";
cin >> word1; // user enters "Hot Tamale"
// word1 has "Hot"
phrase = word1 + word2; // phrase has
// "Hot Dog"
phrase += " on a bun";
for (int i = 0; i < 16; i++)
cout << phrase[i]; // displays
// "Hot Dog on a bun"
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
string Member Functions
• Are behind many overloaded operators
• Categories:
• assignment: assign, copy, data
• modification: append, clear, erase, insert,
replace, swap
• space management: capacity, empty,
length, resize, size
• substrings: find, front, back, at, substr
• comparison: compare
• See Table 10-8 for a list of functions.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
string Member Functions
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
string Member Functions in Program 10-21
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.