1 / 39

Cannon_Chapter9

Cannon_Chapter9. Strings and the string Class. Overview. Standards for Strings String Declarations and Assignment I/O with string Variables string Operations and Comparisons Functions for string Parameter Passing with Strings. String Literal. A string literal is a constant string.

lcenter
Download Presentation

Cannon_Chapter9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cannon_Chapter9 Strings and the string Class

  2. Overview • Standards for Strings • String Declarations and Assignment • I/O with stringVariables • string Operationsand Comparisons • Functions for string • Parameter Passing with Strings

  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");

  4. Standards for Strings #include<iostream.h> #include <string.h> Standard form • With Microsoft Visual C++ #include<iostream> #include <string> using namespace std; Microsoft form

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

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

  7. String vs.char • String constants or literals are represented with doublequotes such as "A". • String literal may contain only a single character. • Character constants are represented with single quotes such as 'A'.

  8. Automatic Conversion string s; char c; s = "%"; c = '%'; s = '%'; // OK c = "%"; // ILLEGAL s = c; // OK c = s; // ILLEGAL

  9. String vs.float • The difference between a number and a string literal containing the digit characters of that number • There is no automatic or default conversion: string text = "123.45"; float number = 123.45; number = text; // ILLEGAL text = number; // ILLEGAL

  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

  11. Blank String • A blank is not “nothing”; it is a specific character. • Output: string word1 = "hello", space = " ", word2 = "world"; cout << word1 << space << word2;

  12. << operator with string variables • Output: • Output: string word1 = "hello", word2 = "world"; cout << word1 << word2; string word1 = "hello", word2 = " world"; cout << word1 << word2;

  13. >> operator with string variables • The sequence of characters up to the next white spaceis 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"

  14. I/O with string variables(1) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;

  15. I/O with string variables(2) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;

  16. I/O with string variables(3) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;

  17. I/O with string variables(4) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;

  18. I/O with string variables(5) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;

  19. I/O with string variables(6) • Output: string word1, word2; cout << "User Inputs: "; cin >> word1 >> word2; cout << "word1 = " << word1 << endl; cout << "word2 = " << word2;

  20. Example • Write a C++ program that • Input are first name, followed by a space, followed by lastname. • Output arelastname, followed by a comma and a space, followed by firstname.

  21. Input and Output • Input: Output:

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

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

  24. Input and Output • Input: Output:

  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"

  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"

  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"; name += " Thompson"; //outputs "Jim Thompson"

  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. 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."; }

  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;

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

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

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

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

  37. Conclusion • String is a representation of textual data. • String literal is a constant string. • Null string is a string containing no characters. • char variablescan hold or represent only a single character. • string variable can contain the digit characters of a number.

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

More Related