On completion of chapter 13 students will know how to: Create strings with the C++ string class, manipulate strings with string functions, read a word, or multiple lines from keyboard, use the string class in programs.
Chapter 13 – C++ String Class String objects Do not need to specify size of string object – C++ keeps track of size of text – C++ expands memory region to store text as needed Can use operators to perform some string manipulations Lesson 13.1 Declaring string Objects Use the class name string and list object names Example: string s1, s2, s3; – s1, s2, s3 would be string objects String header needed to declare string objects Do NOT specify size (Note: no brackets) Initializing string Objects Can initialize with the = – C++ automatically reserves sufficient memory s1 = "This is an example."; Can also place in ( ) but still need " " Null character not required for string text Data member of string class stores size of text Lesson 13.1 Operators + string Objects Type Operator Assignment = += Comparison == != > < >= > > s1; – Reads characters until whitespace typed – Whitespace includes space and "Enter" key Amount of memory for s1 automatically made sufficient Lesson 13.3 Reading Multiple Lines Use function getline – Contained in General form: getline (cin, ob1, 'terminator'); – Ob1 is name of string object – Terminator is terminating character Read but not included in ob1 object Default value is '\n' Read single line of input: getline (cin, ob1); Lesson 13.3 Reading an Input File Use getline for complete file – Read each line into 1D array of string objects Use for loop General form: for ( int j = 0; j