CIS 22B - Notes for Tuesday 2/6

Announcements and Reminders

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.
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.  Match the output exactly.  The team names are displayed in a column of width 24 and the scores are in a column of 6.

Input File
Date,Start (ET),Visitor/Neutral,PTS,Home/Neutral,PTS,,,Notes
Mon Jan 1 2018,7:30 pm,Orlando Magic,95,Brooklyn Nets,98,Box Score,,
Mon Jan 1 2018,8:00 pm,Portland Trail Blazers,124,Chicago Bulls,120,Box Score,OT,
Mon Jan 1 2018,8:00 pm,Los Angeles Lakers,96,Minnesota Timberwolves,114,Box Score,,
Mon Jan 1 2018,7:30 pm,Milwaukee Bucks,127,Toronto Raptors,131,Box Score,OT,
Tue Jan 2 2018,7:00 pm,Portland Trail Blazers,110,Cleveland Cavaliers,127,Box Score,,
Tue Jan 2 2018,10:30 pm,Memphis Grizzlies,105,Los Angeles Clippers,113,Box Score,,
Tue Jan 2 2018,7:30 pm,San Antonio Spurs,100,New York Knicks,91,Box Score,,
Tue Jan 2 2018,9:00 pm,Atlanta Hawks,103,Phoenix Suns,104,Box Score,,
Tue Jan 2 2018,10:00 pm,Charlotte Hornets,131,Sacramento Kings,111,Box Score,,


Output File
Orlando Magic           95    Brooklyn Nets           98
Portland Trail Blazers  124   Chicago Bulls           120
Los Angeles Lakers      96    Minnesota Timberwolves  114
Milwaukee Bucks         127   Toronto Raptors         131
Portland Trail Blazers  110   Cleveland Cavaliers     127
Memphis Grizzlies       105   Los Angeles Clippers    113
San Antonio Spurs       100   New York Knicks         91
Atlanta Hawks           103   Phoenix Suns            104
Charlotte Hornets       131   Sacramento Kings        111