string Objects - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

string Objects

Description:

The find(pattern, position) and. rfind(pattern, position) methods. These do pattern matching ... Returns the index of where it is found (returns value of string: ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 11
Provided by: steve1846
Category:
Tags: find | objects | string | the

less

Transcript and Presenter's Notes

Title: string Objects


1
string Objects
  • string is the name of a class
  • Thus it is a typestring lastName
  • Used in a declaration
  • Creates a string object
  • Initializes it to an empty string
  • Other declarations(with initialization)string
    play "Hamlet"

2
string I/O
  • The string class has overloaded theltlt and gtgt
    operators
  • Can do I/O directly with string objectsstring
    name, prompt "Enter name "cout ltlt
    promptcin gtgt name
  • string input and whitespace
  • Skips leading whitespace characters
  • Quits reading when whitespace encountered
  • Input use getline(). (page 240-241)

3
Other string Operations
  • Subscript operation
  • Access individual characters of a stringstring
    name "John Doe"cout ltlt name 2
  • Size method
  • Determine numberof characters in the stringcout
    ltlt name.size()
  • Empty method cout ltlt name.empty()
  • Returns true if string is empty

4
Other string Operations
  • Assignment operator
  • Assign values to string objectsstring yesterday,
    today "Monday"
  • Relational operator
  • Elements of string operands compared character by
    character
  • Out-of-bound exception

5
Other string Operations
  • Concatenation
  • Combining two strings into a single stringstring
    state "Michigan", greatLakegreatLake "Lake
    "state
  • The substr(first, num_chars) method
  • Returns a string from first for num_charscout ltlt
    greatLake.substr(2,4)

What gets printed?
6
Other string Operations
  • The replace(first, num_chars, replace) method
  • Replaces the contents of the replace string
    starting at first, for num_chars
  • Given
  • What is the end result offullname.replace(5,6,"Ey
    re")

7
Other string Operations
  • The insert(position, new_string) method
  • Inserts a substring into a string
  • Given
  • What is the result ofsignature.insert (5,"E. ")

8
Other string Operations
  • The find(pattern, position) and rfind(pattern,
    position) methods
  • These do pattern matching
  • Searches for the specified pattern starting at
    the given position
  • Returns the index of where it is found (returns
    value of stringnpos if not found)
  • rfind searches from right to left
  • The find_first_of(pattern, position)method
  • Looks for first character from any in pattern
    starting at position

9
Problem
  • Using OCD, design and implement a function that,
  • Given a string containing a long-distance
    telephone number,
  • Decomposes that number into its
  • Area code (if the first number is a 1)
  • Exchange
  • Local number.

Only these two if the first number is not a 1
10
Behavior
Enter Phone number -gt 12345556789 This is a valid
number.Area code 234Exchange 555Local No.
6789
Write a Comment
User Comments (0)
About PowerShow.com