12. Strings - ENGR 101 String Class, Array Semantics ENGR...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGR 101 1 String Class, Array Semantics ENGR 101, Lecture 12: 25 Oct 10 Announcements Project 5 due Wed 27 Oct (9pm) Exam 2 on Wed 3 Nov (6pm) contact Nader Jawad (njawad@umich.edu) about conflicts How is your progress on Project 5? A. Have not started yet B. Wrote some code, not yet compiling/working C. Have one transaction type working D. Have two or more transaction types working E. Completed
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 101 2 The String Class The C++ string class supports construction and manipulation of text objects . To use the string class, we need a standard library: #include <string> Making Strings Literals—just put a series of characters between double quotes: "Barack" Declare a string variable: string firstname; no initial value, so not really a string object (yet) Define a string variable: string firstname("Barack"); or string firstname = "Barack";
Background image of page 2
ENGR 101 3 String Operations Many standard operators are also defined for strings Comparison: > , < , == , >= , <= , != according to lexicographic (dictionary) order using char ordering among characters e.g., all uppercase before lowercase in ASCII Output (insertion): << inserts string on ostream string s1("barack"); string s2("Michelle"); if (s1 < s2) cout << s1 << endl; else cout << s2 << endl; More String Operations Input (extraction): >> Extracts string from istream Cannot read a string with whitespace (why not?) Concatenation: + Combines two strings string firstname, lastname, fullname; cout << "Enter first and last names: "; cin >> firstname; cin >> lastname; fullname = firstname + " " + lastname;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Some String Methods size() number of char s in string append(string s) appends s to string push_back(char c) appends c to string string mystring("Go"); cout << mystring.size(); mystring.append("Blue"); cout << mystring.size(); mystring.push_back('!'); cout << mystring.size(); Line Input reads a whole line from an input stream, stores in a string useful for extracting input with whitespace Operation Leading whitespace Termination condition Consumes terminating character? >> skipped
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

12. Strings - ENGR 101 String Class, Array Semantics ENGR...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online