string Objects string is the name of a
string Objects string is the name of a class • Thus it is a type string last. Name; Used in a declaration • Creates a string object • Initializes it to an empty string Other declarations (with initialization) string play = "Hamlet"; C++ An Introduction to Computing, 3 rd ed.
string I/O The string class has overloaded the << and >> operators • Can do I/O directly with string objects string name, prompt = "Enter name : "; cout << prompt; cin >> name; string input and whitespace • Skips leading whitespace characters • Quits reading when whitespace encountered • Input use getline(). (page 240 -241) C++ An Introduction to Computing, 3 rd ed.
Other string Operations Subscript operation • Access individual characters of a string name = "John Doe"; cout << name [2]; Size method • Determine number of characters in the string 8 characters cout << name. size(); Empty method cout << name. empty(); • Returns true if string is empty C++ An Introduction to Computing, 3 rd ed.
Other string Operations Assignment operator • Assign values to string objects string yesterday, today = "Monday"; yesterday = today; today = "Tuesday"; string his. Name = "James", her. Name = "Jan"; Relational operator • Elements of string operands compared How does his. Name < her. Name character by character evaluate? True or False? • Out-of-bound exception C++ An Introduction to Computing, 3 rd ed.
Other string Operations Concatenation • Combining two strings into a single string state = "Michigan", great. Lake; great. Lake = "Lake "+state; The substr(first, num_chars) method • Returns a string from first for num_chars cout << great. Lake. substr(2, 4); What gets printed? C++ An Introduction to Computing, 3 rd ed.
Other string Operations The replace(first, method num_chars, replace) • Replaces the contents of the replace string starting at first, for num_chars • Given • What is the end result of Six characters fullname. replace(5, 6, "Eyre"); replaced with four. C++ An Introduction to Computing, 3 rd ed.
Other string Operations The insert(position, new_string) • Inserts a substring into a string • Given • What is the result of signature. insert (5, "E. "); C++ An Introduction to Computing, 3 rd ed. method
Other string Operations The find(pattern, position) and rfind(pattern, position) methods • These do pattern matching • Searches for the specified pattern starting at the given position • Returns the index of where it is found (returns value of string: : npos if not found) • rfind searches from right to left The find_first_of(pattern, position)method • Looks for first character from any in pattern starting at position C++ An Introduction to Computing, 3 rd ed.
Problem Using OCD, design and implement a function that, • Given a string containing a long-distance telephone number, • Decomposes that number into its • Area code (if the first number is a 1) • Exchange Only these two if the first number is not a 1 • Local number. C++ An Introduction to Computing, 3 rd ed.
Behavior Enter Phone number -> 12345556789 This is a valid number. Area code : 234 Exchange : 555 Local No. : 6789 C++ An Introduction to Computing, 3 rd ed.
- Slides: 10