String Expressions
|
|
|
|
|
For now, one string operator,
concatenation, “+” |
|
Consider |
|
string bookTitle; |
|
string phrase1; string phrase2; |
|
phrase1 = “Programming and ”; |
|
phrase2 = “Problem Solving”; |
|
bookTitle = phrase1 + phrase2; |
|
bookTitle has “Programming and Problem
Solving” stored |
|
bookTitle = phrase2 + phrase1; |
|
bookTitle now has “Problem
SolvingProgramming and ” stored |
|
Warning: At least one of the operands
must be a string variable or named constant. |
Output
|
|
|
|
We use cout and the insertion operator
“<<“. |
|
cout << “Hello”; |
|
cout displays the string inserted
characters on the standard output device, usually the video screen. |
|
cout << “The title is ” <<
bookTitle + “ , 2nd Edition”; |
|
OutputStatement |
|
cout << Expression <<
Expression …; |
Output Examples
|
|
|
Initializations ch = ‘2’ firstName =
“Marie” |
|
lastName = “Curie” |
|
Statement What Is
Printed |
|
( means
blank) |
|
cout << ch; 2 |
|
cout << “ch = ” <<
ch; ch=2 |
|
cout << firstName + “ ” +
lastName; MarieCurie |
|
cout << firstName <<
lastName; MarieCurie |
|
cout << firstName << ‘ ’
<< lastName; MarieCurie |
|
cout << “ERROR
MESSAGE”; ERRORMESSAGE |
|
cout << “Error=” <<
ch; Error=2 |
Program Construction
|
|
|
Program FunctionDefinition |
One Function Program
Example
|
|
|
//************************************************************ |
|
// PrintName program |
|
// This program prints a name in two
different formats |
|
//************************************************************ |
|
#include <iostream> |
|
#include <string> |
|
|
|
using namespace std; |
|
|
|
const string FIRST =
"Herman"; // Person's
first name |
|
const string LAST =
"Smith"; // Person's
last name |
|
const char MIDDLE = 'G'; //
Person's middle initial |
|
|
|
|
Program Example
(continued)
|
|
|
int main() |
|
{ |
|
string firstLast; // Name in
first-last format |
|
string lastFirst; // Name in
last-first format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout << "Name in first-last format is " <<
firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout << "Name in last-first-initial format is "; |
|
cout << lastFirst << MIDDLE << '.' << endl; |
|
|
|
return 0; |
|
} |
|
|
Program Example
(continued)
|
|
|
int main() |
|
{ |
|
string firstLast; // Name in
first-last format |
|
string lastFirst; // Name in
last-first format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout << "Name in first-last format is " <<
firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout << "Name in last-first-initial format is "; |
|
cout << lastFirst << MIDDLE << '.' << endl; |
|
|
|
return 0; |
|
} |
|
|
Program Example
(continued)
|
|
|
int main() |
|
{ |
|
string firstLast; // Name in
first-last format |
|
string lastFirst; // Name in
last-first format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout << "Name in first-last format is " <<
firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout << "Name in last-first-initial format is "; |
|
cout << lastFirst << MIDDLE << '.' << endl; |
|
|
|
return 0; |
|
} |
|
|
Program Example
(continued)
|
|
|
int main() |
|
{ |
|
string firstLast; // Name in
first-last format |
|
string lastFirst; // Name in
last-first format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout << "Name in first-last format is " <<
firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout << "Name in last-first-initial format is "; |
|
cout << lastFirst << MIDDLE << '.' << endl; |
|
|
|
return 0; |
|
} |
|
|
Program Example
(continued)
|
|
|
int main() |
|
{ |
|
string firstLast; // Name in
first-last format |
|
string lastFirst; // Name in
last-first format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout << "Name in first-last format is " <<
firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout << "Name in last-first-initial format is "; |
|
cout << lastFirst << MIDDLE << '.' << endl; |
|
|
|
return 0; |
|
} |
|
|
Program Example
(continued)
|
|
|
int main() |
|
{ |
|
string firstLast; // Name in
first-last format |
|
string lastFirst; // Name in
last-first format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout << "Name in first-last format is " <<
firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout << "Name in last-first-initial format is "; |
|
cout << lastFirst << MIDDLE << '.' << endl; |
|
|
|
return 0; |
|
} |
|
|
Program Example
(continued)
|
|
|
int main() |
|
{ |
|
string firstLast; // Name in
first-last format |
|
string lastFirst; // Name in
last-first format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout << "Name in first-last format is " <<
firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout << "Name in last-first-initial format is "; |
|
cout << lastFirst << MIDDLE << '.' << endl; |
|
|
|
return 0; |
|
} |
|
|
Program Example
(continued)
|
|
|
int main() |
|
{ |
|
string firstLast; // Name in
first-last format |
|
string lastFirst; // Name in
last-first format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout << "Name in first-last format is " <<
firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout << "Name in last-first-initial format is "; |
|
cout << lastFirst << MIDDLE << '.' << endl; |
|
|
|
return 0; |
|
} |
|
|
More Syntax
More About Output
|
|
|
Statements |
|
cout << “Hi there, ”; |
|
cout << “Lois Lane. ”
<< endl; |
|
cout << “Have you seen ”; |
|
cout << “Clark Kent?”
<< endl; |
|
Output |
|
Hi there, Lois Lane. |
|
Have you seen Clark Kent? |
|
|
Still More About Output
|
|
|
Statements |
|
cout << “Hi there, ” <<
endl; |
|
cout << “Lois Lane. ”
<< endl; |
|
cout << “Have you seen ”
<< endl; |
|
cout << “Clark Kent?”
<< endl; |
|
Output |
|
Hi there, |
|
Lois Lane. |
|
Have you seen |
|
Clark Kent? |
|
|
|
|
Another Output Example
|
|
|
Statements |
|
cout << “Hi there, ” <<
endl; |
|
cout << “Lois Lane. ”; |
|
cout << “Have you seen ”
<< endl; |
|
cout << “Clark Kent?”
<< endl; |
|
Output |
|
Hi there, |
|
Lois Lane. Have you seen |
|
Clark Kent? |
|
|
Last Output Example For
Now
|
|
|
Statements |
|
cout << “Hi there, ”; |
|
cout << “Lois Lane. ”; |
|
cout << “Have you seen ”; |
|
cout << “Clark Kent?”
<< endl; |
|
Output |
|
Hi there, Lois Lane. Have you seen
Clark Kent? |
|
|
Problem-Solving Case
Study
|
|
|
Problem: You must write a program to
write personalized contest entry letters. As a first effort, you need it to
work for one name. Later, it will read names from a mailing list file. |
|
Output: A form letter with a name
inserted at the appropriate point so that it appears to be a personal letter. |
|
Discussion: You need to insert a name
in the marketing department’s text. The parts of a person’s name need to be
stored separately, different parts are used in different places. You need to
use the string concatenation operator. |
|
|
Algorithm
Form Letter Program
|
|
|
//************************************************************ |
|
// FormLetter program |
|
// This program prints a form letter
for a promotional contest. |
|
// It uses the four parts of a name to
build name strings in |
|
// four different formats to be used in
personalizing the letter |
|
//************************************************************** |
|
|
|
#include <iostream> |
|
#include <string> |
|
|
|
using namespace std; |
|
|
|
const string TITLE =
"Dr."; //
Salutary title |
|
const string FIRST_NAME =
"Margaret"; // First name
of addressee |
|
const string MIDDLE_INITIAL =
"H"; // Middle initial |
|
const string LAST_NAME =
"Sklaznick"; // Last name
of addressee |
Form Letter (continued)
|
|
|
int main() |
|
{ |
|
string first; // Holds
the first name plus a blank |
|
string fullName; //
Complete name, including title |
|
string firstLast; // First
name and last name |
|
string titleLast; // Title
followed by the last name |
|
|
|
// Create first name with blank |
|
first = FIRST_NAME + " "; |
|
|
|
// Create full name |
|
fullName = TITLE + " " + first + MIDDLE_INITIAL; |
|
fullName = fullName + ". " + LAST_NAME; |
|
|
|
// Create first and last name |
|
firstLast = first + LAST_NAME; |
Form Letter (continued)
|
|
|
// Create title and last name |
|
titleLast = TITLE + " " + LAST_NAME; |
|
|
|
// Print the form letter |
|
cout << fullName << " is a GRAND PRIZE
WINNER!!!!" << endl<< endl; |
|
cout << "Dear " << titleLast <<
"," << endl << endl; |
|
cout << "Yes it's true! " << firstLast <<
" has won our" << endl; |
|
cout << "GRAND PRIZE -- your choice of a 42-INCH*
COLOR" << endl; |
|
cout << "TV or a FREE WEEKEND IN NEW YORK CITY.**"
<< endl; |
|
cout << "All that you have to do to collect your prize
is”<< endl; |
|
cout << "attend one fun-filled all-day presentations”
<< endl; |
|
cout << "on the joys of owning a timeshare
condominium" << endl; |
|
cout << "trailer at the Happy Acres Mobile Campground in“
<< endl; |
|
cout << "beautiful Panhard, Texas!" << first
<< "I know” << endl; |
|
cout << "that the 3-hour drive from the nearest airport”
<< endl; |
|
cout << "to Panhard may seem daunting at first, but
isn't" <<endl; |
|
|
Yet More Form Letter
|
|
|
cout << "it worth some
extra effort to receive such a" << endl; |
|
cout << "FABULOUS
PRIZE? So why wait? Call us right" << endl; |
|
cout << "now to
schedule your visit and collect your" << endl; |
|
cout << "GRAND PRIZE!" << endl << endl; |
|
cout << "Most Sincerely," << endl << endl; |
|
cout << "Argyle M. Sneeze" << endl << endl
<< endl << endl; |
|
cout << "* Around the circumference of the packing”
<< endl; |
|
cout << "crate. ** Includes air fare and hotel."
<< endl; |
|
cout << "Departure from Nome, AK; surcharge from” << endl; |
|
cout << "other airports. Accommodations within” << endl; |
|
cout << "driving distance of New York at the CheapTel”
<< endl; |
|
cout << "in Plattsburgh, NY." << endl; |
|
return 0; |
|
} |