Strings in C - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Strings in C

Description:

to use string CONSTANTS in double quotes, just use them - no library needed ... returns leftmost position found. or string::npos if not found. find method ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 12
Provided by: Deb79
Category:
Tags: leftmost | strings

less

Transcript and Presenter's Notes

Title: Strings in C


1
Strings in C

2
To use "new" strings in C
  • to use string CONSTANTS in double quotes, just
    use them - no library needed
  • to use string variables and functions, you must
    include ltstringgt
  • declare string variables
  • assignment statement
  • input with extraction operator
  • output with insertion operator

3
String Operators and Methods
  • concatenation
  • dot notation
  • length()
  • substr()
  • find()

4
concatenation
  • operator is
  • at least one of the two operands must be a string
    variable
  • example call if name Bob, expression
  • name "by Roberts" results in one string
    "Bobby Roberts"
  • does not automatically put spaces in

5
length method
  • Function length returns an unsigned integer value
    that equals the number of characters currently in
    the string
  • Function size returns the same value as function
    length
  • You must use dot notation in the call to function
    length or size

6
length method
  • no arguments, just ()
  • smallest return possible is 0 (empty string)
  • treat return type as an integer
  • word Johnson
  • x word.length() // gives x value 7

7
substr method
  • two arguments usually
  • example call name.substr(4, 3)
  • first argument is position to start the string
  • Positions of characters within a string are
    numbered starting from 0, not 1
  • second argument is how many characters
  • if second argument is omitted, means "until end
    of string"
  • returns a string (NOT a character)

8
find method
  • one argument
  • example call name.find("Andy")
  • argument can be string or character
  • finds FIRST one from the left
  • case matters!
  • returns integer (actually size_t)
  • returns leftmost position found
  • or stringnpos if not found

9
find method
  • Function find returns an unsigned integer value
    that is the beginning position for the first
    occurrence of a particular substring within the
    string
  • The substring argument can be a string constant,
    a string expression, or a char value
  • If the substring was not found, function find
    returns the special value stringnpos

10
String Methods - Examples
  • string street "123 Main Street"
  • string number, st // declarations
  • int stlen, space_pos, x_pos
  • stlen street.length() // 15
  • space_pos street.find (" ") // 3
  • x_pos street.find ("x") // npos
  • number street.substr(0,3) // "123"
  • st street.substr(9) // "Street"

11
Subscript notation
  • Another way to access individual elements of a
    string is with a subscript
  • If word joe, then word0 is j
  • A character, not a smaller string
  • word1 is o and word2 is e
  • Be very careful about assigning string elements
    values like this sometimes the length field is
    not correctly updated
Write a Comment
User Comments (0)
About PowerShow.com