Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables "— Presentation transcript:

1 1 Cannon_Chapter9 Strings and the string Class

2 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables  string Operations and Comparisons  Functions for string  Parameter Passing with Strings

3 3 String Literal  A 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");

4 4 #include using namespace std; #include using namespace std; #include Standards for Strings Standard form  With Microsoft Visual C++ Microsoft form

5 5 String Declarations  Variables are declared with the data type string  Example string variable, variable,..; string word1, word2 = "hello", word3 = "goodbye";

6 6 String Assignment  string variables can be assigned from other string variables  Example word1 = "Hello World"; word2 = word1; word1 = "Hello World"; word2 = word1;

7 7 String vs. char  String 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'.

8 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

9 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:

10 10 Null String  The null string is just a string containing no characters.  The null string is not the same as a string containing only blanks.  Example string 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

11 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:

12 12 << operator with string variables  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;

13 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"

14 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:

15 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:

16 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:

17 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:

18 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:

19 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:

20 20 Example  Write 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.

21 21 Input and Output  Input: Output:

22 22 C++ Program #include void main() { ifstream infile ("instructor.txt", ios::in); string firstname, lastname; while(infile >> firstname >> lastname) { cout << lastname << ", " << firstname << endl; } #include void main() { ifstream infile ("instructor.txt", ios::in); string firstname, lastname; while(infile >> firstname >> lastname) { cout << lastname << ", " << firstname << endl; }

23 23 Function getline()  Read an entire line of text input to a string variable including white spaces or blanks #include void main() { ifstream infile ("instructor.txt", ios::in); string name; while(getline(infile, name)) cout << name << endl; } #include void main() { ifstream infile ("instructor.txt", ios::in); string name; while(getline(infile, name)) cout << name << endl; }

24 24 Input and Output  Input: Output:

25 25 string Operations  A string variable may be concatenated with another string using the + operator.  Concatenation means that the two strings are joined or pasted together.  Example string 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"

26 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"

27 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"

28 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")...//ILLEGAL

29 29 Example #include 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 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."; }

30 30 Functions for string  The 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;

31 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!";

32 32 Functions for string (cont.)  The find() function doesn’t look for an independent word – just the sequence of characters J, i and m

33 33 Example #include 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 const string word = "nuclear"; const string phrase = "atomic energy"; void main() { string line; ifstream infile; infile.open("test.txt"); //Correct error in text book

34 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; }

35 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.

36 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; }

37 37 Conclusion  String 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.

38 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.

39 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.


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

Similar presentations


Ads by Google