Title: Chapter 13
1Chapter 13 C String Class
2String 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
3Declaring 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)
4Initializing string Objects
- Can initialize with the
- C automatically reserves sufficient memory
- Can also place in ( ) but still need " "
- Null character not required for string text
- Data member of string class stores size of text
s1 "This is an example."
Lesson 13.1
5Operators string Objects
Type Operator Action Assignment Stores
string Concatenates and stores Comparison
True if strings identical ! True if strings
not identical gt True if first string greater
than second lt True if first string is less
than second gt True if first string greater or
equal than second lt True if first string less
or equal than second Input/Output gtgt For input
and string objects ltlt For output and string
objects Character To access individual
characters access Concatenation
Connects two strings
Lesson 13.1
6C Strings vs. string Objects
- Older code uses C strings
- C strings more basic faster execution
- String class improves ability to manipulate text
safely - Sized automatically
- No null necessary
- Easier modification in program
Lesson 13.1
7Some Member Functions
- More actions needed than operators can provide
- Calling member function involves using object
name with dot operator and function name - Invoking object
- One that is modified
Lesson 13.2
8find Function
- Searches for a string within a string
- Basic form of call to find ob1.find
(ob2) - finds first occurrence of string ob2 within ob1
- Returns position
0 1 2 3 4 5 6 7 8 9 0 1
s1 "This is an example." s2 "exam" n
s1.find (s2)
The return value is 11
Lesson 13.2
9Overloaded find Function
- Another version has two arguments
- Basic form ob1.find (ob2, index)
- index represents integer value for beginning of
search - C performs automatic type conversion from C
strings to string objects when C strings are in
string function call - String not found returns -1
Lesson 13.2
10replace Function
- Replaces characters within a string object with
another string - Returns reference to invoking object
- Basic form ob1.replace (index, num, ob2)
- index represents position to begin
- num is the number of characters to replace
- ob2 what is to be used to replace (can be C
string) - Overloaded ob1.replace (index1, num1, ob2,
index2, num2)
Lesson 13.2
11erase Function
- Eliminates characters within string object
- Basic form ob1.erase (index, num)
- num represents number of characters to erase
- index is the beginning position
- Returns reference to the invoking object
s1 "This is an example." s1.erase (8,3)
example."
Lesson 13.2
12insert Function
- Adds characters to a string object
- Basic form ob1.insert (index, ob2)
- index is beginning position
- ob2 represents what is to be inserted
- Can be a C string
- Returns reference to the invoking function
s1 "This is an example." s1.insert (8,"just ")
just an example."
Lesson 13.2
13Other string Functions
- Many other functions available
- Examples compare, append, resize, etc.
- Described in text, table 13.2
- List of functions
- Sample syntax
- What they return
- Description of purpose
Lesson 13.2
14Reading a Single Word
- From keyboard using cin cin gtgt s1
- Reads characters until whitespace typed
- Whitespace includes space and "Enter" key
- Amount of memory for s1 automatically made
sufficient
Lesson 13.3
15Reading Multiple Lines
- Use function getline
- Contained in ltstringgt
- 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
16Reading an Input File
- Use getline for complete file
- Read each line into 1-D array of string objects
- Use for loop
- General form for ( int j 0 j lt
num_lines j)
getline (infile, array_element)
Lesson 13.3
17Strings and Functions
string function1 (string, const string ,
string, string )
- Return type for function is string object
- string objects passed like other objects
- Copy is passed when type string is argument
- Keyword const prevents array elements from being
modified - indicates a reference
Lesson 13.4
18Class Definition Example
class Class1 private
char aa 20
string s1 public
char get_aa( ) string
get_s1 ( ) void read_data (
)
Lesson 13.5
19Member Functions
void Class1 read_data ( ) cout
ltlt "Enter your name." ltlt endl
getline (cin ,s1) coutltlt "Enter phone
number." ltltendl cin.getline
(aa)
class Class1 private
char aa 20
string s1 public
char get_aa( ) string
get_s1 ( ) void read_data (
)
char Class1 get_aa ( )
return aa
string Class1 get_s1 ( )
return s1
Lesson 13.5
20Working With a Class
- Object of class declared allows member
functions to be called - Example Class1 ob1ob1.read_data ( )cout
ltltob1.get_s1ltltendlltltob1.get_aa ( ) ltltendl
Lesson 13.5
21Summary
Learned 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