39
1 Cannon_Chapter9 Strings and the Strings and the string string Class Class

1 Cannon_Chapter9 Strings and the string Class. 2 Overview Standards for Strings String Declarations and Assignment I/O with string Variables

Embed Size (px)

Citation preview

Page 1: 1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.