CIS 22B - Notes for Mon Oct 23

Announcements and Reminders

  • Assignment 4 is due NOW
  • On-line time tomorrow night 7:00-8:15 pm  CCC Confer.
  • Free flu shot Wednesday & Thursday this week in the Don Bautista room upstairs in the Campus Center, 10 am to 2 p.m.

C++ string class

Reference

Member Functions Prototype Description
constructor string();
string(const char* str);
string(const str& str);
Creates a string object
size
length
size()
length()
Returns the lenth of the string
clear clear() Clears the string
c_str c_str() Returns the null-terminated char array contents of the string
find size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;    
size_t find (char c, size_t pos = 0) const;
Locates text in a string.  Returns npos if not found
substr string substr (size_t pos = 0, size_t len = npos) const; Returns a substring of the string object
Member Operators
= assignment operator= (const string& str);    
string& operator= (const char* s);
string& operator= (char c);
Assigns a value to a string
[ ] index       char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
Returns the char at a location in the string
+= plus equal string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);
Concatenates text to a string
Non-member Operators
+  plus string operator+ (const string& lhs, const string& rhs);
string operator+ (const string& lhs, const char*   rhs);
string operator+ (const char*   lhs, const string& rhs);    
string operator+ (const string& lhs, char          rhs);
string operator+ (char          lhs, const string& rhs);
Concatenates a string and text.  Returns a new string.
Non-member functions
getline istream& getline (istream& in, string& str, char delim);
istream& getline (istream& in, string& str);
Reads from an input stream into a string
Member Constants
npos Indicates not found

Example

Read Me


Lab Exercise #5

Put your name, the compiler used, and Lab Exercise #5 in a comment at the top of your program. Email your source code. This lab exercise is due at the beginning of the next lecture.  You may email your lab solution prior to that.

Write a program to read the input file, shown below and write out the output file shown below.  Use only string objects and string functions to process the data.  Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution.  

Input File
Cincinnati 27, Buffalo 24
Detroit 31, Cleveland 17
Kansas City 24, Oakland 7
Carolina 35, Minnesota 10
Pittsburgh 19, NY Jets 6
Philadelphia 31, Tampa Bay 20
Green Bay 19, Baltimore 17
St. Louis 38, Houston 13
Denver 35, Jacksonville 19
Seattle 20, Tennessee 13
New England 30, New Orleans 27
San Francisco 32, Arizona 20
Dallas 31, Washington 16


Output File
Cincinnati over Buffalo 27 to 24
Detroit over Cleveland 31 to 17
Kansas City over Oakland 24 to 7
Carolina over Minnesota 35 to 10
Pittsburgh over NY Jets 19 to 6
Philadelphia over Tampa Bay 31 to 20
Green Bay over Baltimore 19 to 17
St. Louis over Houston 38 to 13
Denver over Jacksonville 35 to 19
Seattle over Tennessee 20 to 13
New England over New Orleans 30 to 27
San Francisco over Arizona 32 to 20
Dallas over Washington 31 to 16