1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String...
-
Upload
jack-lucas -
Category
Documents
-
view
225 -
download
0
Transcript of 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String...
![Page 1: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/1.jpg)
1
Cannon_Chapter9
Strings and the Strings and the stringstring ClassClass
![Page 2: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/2.jpg)
2
OverviewStandards for StringsString Declarations and Assignment
I/O with string Variables string Operations and ComparisonsFunctions for string Parameter Passing with Strings
![Page 3: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/3.jpg)
3
String LiteralA string literal is a constant string.
Example
A string is a sequence of characters such as a sentence or a single word.
cout << "Hello World!";
infile.open("input.txt");
cout << "Hello World!";
infile.open("input.txt");
![Page 4: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/4.jpg)
4
#include <iostream>
#include <string>
using namespace std;
#include <iostream>
#include <string>
using namespace std;
#include <iostream.h>
#include <string.h>
#include <iostream.h>
#include <string.h>
Standards for Strings
Standard form
With Microsoft Visual C++
Microsoft form
![Page 5: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/5.jpg)
5
String Declarations
Variables are declared with the data type string
Example
string variable, variable, ..;string variable, variable, ..;
string word1, word2 = "hello", word3 = "goodbye";
string word1, word2 = "hello", word3 = "goodbye";
![Page 6: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/6.jpg)
6
String Assignment string variables can be assigned from other string variables
Exampleword1 = "Hello World";
word2 = word1;
word1 = "Hello World";
word2 = word1;
![Page 7: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/7.jpg)
7
String vs. charString constants or literals are represented with double quotes such as "A".
String literal may contain only a single character.
Character constants are represented with single quotes such as 'A'.
![Page 8: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/8.jpg)
8
Automatic Conversion
string s;
char c;
s = "%";
c = '%';
s = '%'; // OK
c = "%"; // ILLEGAL
s = c; // OK
c = s; // ILLEGAL
string s;
char c;
s = "%";
c = '%';
s = '%'; // OK
c = "%"; // ILLEGAL
s = c; // OK
c = s; // ILLEGAL
![Page 9: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/9.jpg)
9
String vs. float
string text = "123.45";
float number = 123.45;
string text = "123.45";
float number = 123.45;
number = text; // ILLEGAL
text = number; // ILLEGAL
number = text; // ILLEGAL
text = number; // ILLEGAL
The difference between a number and a string literal containing the digit characters of that number
There is no automatic or default conversion:
![Page 10: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/10.jpg)
10
Null StringThe null string is just a string containing no characters.
The null string is not the same as a string containing only blanks.
Examplestring str; // str contains the null string
string word = ""; // word contains the null string
word = " "; // word now contains a blank
string str; // str contains the null string
string word = ""; // word contains the null string
word = " "; // word now contains a blank
![Page 11: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/11.jpg)
11
Blank String
string word1 = "hello", space = " ", word2 = "world";
cout << word1 << space << word2;
string word1 = "hello", space = " ", word2 = "world";
cout << word1 << space << word2;
A blank is not “nothing”; it is a specific character.
Output:
![Page 12: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/12.jpg)
12
<< operator with string variables
Output:
Output:
string word1 = "hello", word2 = "world";
cout << word1 << word2;
string word1 = "hello", word2 = "world";
cout << word1 << word2;
string word1 = "hello", word2 = " world";
cout << word1 << word2;
string word1 = "hello", word2 = " world";
cout << word1 << word2;
![Page 13: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/13.jpg)
13
>> operator with string variables
The sequence of characters up to the next white space is placed within the variable.
Example: If user types "hello world",
string word;
cin >> word; //word contains "hello"
string word1, word2;
cin >> word1 >> word2; //word1 contains "hello" //word2 contains "world"
string word;
cin >> word; //word contains "hello"
string word1, word2;
cin >> word1 >> word2; //word1 contains "hello" //word2 contains "world"
![Page 14: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/14.jpg)
14
I/O with string variables (1)
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
Output:
![Page 15: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/15.jpg)
15
I/O with string variables (2)
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
Output:
![Page 16: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/16.jpg)
16
I/O with string variables (3)
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
Output:
![Page 17: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/17.jpg)
17
I/O with string variables (4)
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
Output:
![Page 18: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/18.jpg)
18
I/O with string variables (5)
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
Output:
![Page 19: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/19.jpg)
19
I/O with string variables (6)
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
string word1, word2;
cout << "User Inputs: ";
cin >> word1 >> word2;
cout << "word1 = " << word1 << endl;
cout << "word2 = " << word2;
Output:
![Page 20: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/20.jpg)
20
ExampleWrite a C++ program that
•Input are first name, followed by a space, followed by last name.•Output are last name, followed by a comma and a space, followed by first name.
![Page 21: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/21.jpg)
21
Input and Output Input: Output:
![Page 22: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/22.jpg)
22
C++ Program#include <iostream.h>
#include <fstream.h>
#include <string>
void main()
{
ifstream infile ("instructor.txt", ios::in);
string firstname, lastname;
while(infile >> firstname >> lastname)
{
cout << lastname << ", " << firstname << endl;
}
}
#include <iostream.h>
#include <fstream.h>
#include <string>
void main()
{
ifstream infile ("instructor.txt", ios::in);
string firstname, lastname;
while(infile >> firstname >> lastname)
{
cout << lastname << ", " << firstname << endl;
}
}
![Page 23: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/23.jpg)
23
Function getline()
Read an entire line of text input to a string variable including white spaces or blanks#include <iostream.h>
#include <fstream.h>
#include <string>
void main()
{
ifstream infile ("instructor.txt", ios::in);
string name;
while(getline(infile, name))
cout << name << endl;
}
#include <iostream.h>
#include <fstream.h>
#include <string>
void main()
{
ifstream infile ("instructor.txt", ios::in);
string name;
while(getline(infile, name))
cout << name << endl;
}
![Page 24: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/24.jpg)
24
Input and Output Input:
Output:
![Page 25: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/25.jpg)
25
string OperationsA string variable may be concatenated with another string using the + operator.
Concatenation means that the two strings are joined or pasted together.
Examplestring first = "Jim", last = " Thompson", name;
name = first + last;
cout << name << endl; //outputs "Jim Thompson"
string first = "Jim", last = " Thompson", name;
name = first + last;
cout << name << endl; //outputs "Jim Thompson"
![Page 26: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/26.jpg)
26
string Operations (cont.)
A literal may be concatenated onto the end of a string variable.string first = "Jim", name;
name = first + " Thompson";
cout << name << endl; //outputs "Jim Thompson"
name = "Thompson, " + first;
cout << name << endl; //outputs "Thompson, Jim"
string first = "Jim", name;
name = first + " Thompson";
cout << name << endl; //outputs "Jim Thompson"
name = "Thompson, " + first;
cout << name << endl; //outputs "Thompson, Jim"
![Page 27: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/27.jpg)
27
string Operations (cont.)
Two literals cannot be concatenated into a string.
The shorthand operator += can be used:
string name;
name = "Jim" + " Thompson"; //ILLEGAL
string name;
name = "Jim" + " Thompson"; //ILLEGAL
string name;
name = "Jim";
name += " Thompson"; //outputs "Jim Thompson"
string name;
name = "Jim";
name += " Thompson"; //outputs "Jim Thompson"
![Page 28: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/28.jpg)
28
string Comparisons string variables and literals can be compared using the relational operators (==, !=, <, >, <=, >=).
The first string is less than the second if its character in that position has an ASCII code less than the character in the same position in the other string.
Two literals cannot be compared.if ("Zeek" < "adam")... //ILLEGALif ("Zeek" < "adam")... //ILLEGAL
![Page 29: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/29.jpg)
29
Example#include <iostream.h>
#include <string>
void main()
{
string first, second;
cout << "Enter two words: ";
cin >> first >> second;
if (first == second)
cout << "Words are exactly equal.";
else if (first < second)
cout << "The first word is less than the second.";
else cout << "The second word is less than the first.";
}
#include <iostream.h>
#include <string>
void main()
{
string first, second;
cout << "Enter two words: ";
cin >> first >> second;
if (first == second)
cout << "Words are exactly equal.";
else if (first < second)
cout << "The first word is less than the second.";
else cout << "The second word is less than the first.";
}
![Page 30: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/30.jpg)
30
Functions for stringThe current length of a string variable can be determined using the length() function.
What is the output?
string name = "Jim Thompson";
int size;
size = name.length();
cout << "Length of “ << name << “ is " << size;
string name = "Jim Thompson";
int size;
size = name.length();
cout << "Length of “ << name << “ is " << size;
![Page 31: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/31.jpg)
31
Functions for string (cont.)
The maximum length and a particular word of a string variable can be determined by using the max_size() and find() function respectively.string sentence;
getline(cin, sentence);
if (sentence.find("Jim", 0) < sentence.max_size())
cout << "The word \"Jim\" was found in this sentence!";
else
cout << "The word \"Jim\" was found in this sentence!";
string sentence;
getline(cin, sentence);
if (sentence.find("Jim", 0) < sentence.max_size())
cout << "The word \"Jim\" was found in this sentence!";
else
cout << "The word \"Jim\" was found in this sentence!";
![Page 32: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/32.jpg)
32
Functions for string (cont.)
The find() function doesn’t look for an independent word – just the sequence of characters J, i and m
![Page 33: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/33.jpg)
33
Example#include <iostream.h>
#include <fstream.h>
#include <string>
const string word = "nuclear";
const string phrase = "atomic energy";
void main()
{
string line;
ifstream infile;
infile.open("test.txt");
//Correct error in text book
#include <iostream.h>
#include <fstream.h>
#include <string>
const string word = "nuclear";
const string phrase = "atomic energy";
void main()
{
string line;
ifstream infile;
infile.open("test.txt");
//Correct error in text book
![Page 34: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/34.jpg)
34
Example (cont.)while (getline(infile,line))
{
if (line.find(word, 0) < line.max_size())
cout << "WORD FOUND!" << endl;
else if (line.find(phrase, 0) < line.max_size())
cout << "PHRASE FOUND!" << endl;
}
cout << "File completely examined." << endl;
}
while (getline(infile,line))
{
if (line.find(word, 0) < line.max_size())
cout << "WORD FOUND!" << endl;
else if (line.find(phrase, 0) < line.max_size())
cout << "PHRASE FOUND!" << endl;
}
cout << "File completely examined." << endl;
}
![Page 35: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/35.jpg)
35
Parameter passing with strings
string variables may be used as parameters to functions.
Order() function will swap two strings if not in order and will return the shorter of the two.
![Page 36: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/36.jpg)
36
Parameter passing with strings (cont.)string Order(string& first, string& second)
{
string temp;
if (first >= second)
{
temp = first;
first = second;
second = temp; //Correct error in text book
}
if (first.length() < second.length())
return first;
else return second;
}
string Order(string& first, string& second)
{
string temp;
if (first >= second)
{
temp = first;
first = second;
second = temp; //Correct error in text book
}
if (first.length() < second.length())
return first;
else return second;
}
![Page 37: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/37.jpg)
37
ConclusionString is a representation of textual data.
String literal is a constant string.Null string is a string containing no characters.
char variables can hold or represent only a single character.
string variable can contain the digit characters of a number.
![Page 38: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/38.jpg)
38
Conclusion (cont.)The header file for string access does not have an .h extension.
string variables may be input and output using the >> and << operators.
string variable(s) and string literal(s) can be concatenated using the + operator.
Any of the six relational comparison operators may be used to compare two strings or a string with a literal.
![Page 39: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697bf861a28abf838c8808e/html5/thumbnails/39.jpg)
39
Conclusion (cont.)The length of a string variable can be determined using the length() function.
The maximum length of a string variable can be determined using the max_size() function.
Whether a string variable contains a particular substring or sequence can be tested with the find() function.