CPS120: Introduction to Computer Science - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CPS120: Introduction to Computer Science

Description:

Need to use strcpy(string1, string2); Comparing. Calculating length. string.h ... if (string1 == string2) Test two strings alphabetically. if (string1 string2) ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 22
Provided by: wwwperson4
Learn more at: http://space.wccnet.edu
Category:

less

Transcript and Presenter's Notes

Title: CPS120: Introduction to Computer Science


1
CPS120 Introduction to Computer Science
  • Using a String Class

2
C Includes vs C Inclues
  • Header file names no longer maintain the .h
    extension as in the case of stdio.h, stdlib.h,
    iostream.h, etc. This extension h simply
    disappears and iostream.h becomes iostream
    (without .h).
  • All classes and functions defined in standard
    libraries are under the std namespace instead of
    being global. This not applies to C macros that
    remain as C macros

3
Proper form for C
  • include lt iostream gt
  • include lt string gt
  • using namespace std

4
Use ltstringgt instead of ltstring.hgt
  • ltstring.hgt is the standard C library for
    manipulating C strings, i.e. arrays of char
    terminated with a 0.
  • The string class library header is supposed to be
    called string too. The Standard says that C
    standard header files, such as the string class
    uses, are referenced without the .h extension,
    which is reserved for backward compatibility with
    older C libraries.

5
Using C-Type Strings
6
Strings
  • No native STRING type
  • Use null-character terminated arrays
  • Strings are handled fundamentally differently
  • Assigning You can hardly ever use the ""
  • Need to use strcpy(string1, string2)
  • Comparing
  • Calculating length

7
string.h
  • Gives access to functions for handling strings
    like
  • strlen( )
  • strcat( )
  • strcmp( )
  • strcpy( )

8
Concatenation
  • The STRCAT function will continue to add
    characters even if it reaches the end of the
    destination character array
  • The result is that other memory is overwritten
  • with the remaining string data.

9
Converting Strings
  • There are C library functions that allow you to
    conver4t numbers stored in strings to integer and
    floating point values.

10
Using a String Class
  • Using the features of C

11
String Classes
  • Strings in C are objects
  • There are member functions associated with every
    object of the type string.
  • length( )
  • stringQ.length ( )
  • The dot indicates the operation is being invoked
    on the object to the left of the dot
  • The string class seems more like other native
    data types
  • Strings can be assigned with ""
  • Stings can be added together

12
A String Class
  • A string class can make C strings much easier
    to use
  • a) A string class can provide bounds checking to
    avoid string errors
  • b) A string class can also allow the use of
    standard C operators when working with strings
  • i) , , and

13
A Brave New World
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • int main ( )
  • string Greeting
  • Greeting "Hello World!"
  • cout ltlt Greeting ltlt endl
  • return 0

14
Library functions manipulate strings
  • length function
  • stringLength userString.length( )
  • To compare two strings
  • if (string1 string2)
  • Test two strings alphabetically
  • if (string1 lt string2)
  • cout ltlt "string1 comes before string2
  • alphabetically "

15
Obtaining Length
  • Obtain length
  • Length MyString2.length()

16
Assigning Values to Strings
  • MyString1 MyString2
  • MyString1 "string variable"
  • MyString1 'A'

17
Indexing Characters
  • Indexing characters to a string object
  • MyString10 'B'

18
Comparisons
  • Using a string class you can directly compare two
    strings
  • Previously you had to call a special function
    strcmp( )

19
Substrings
  • Most languages have an operation for extracting
    part of a string
  • In C, you supply the starting position and the
    number of characters to extract
  • substr member function (operator)
  • stringA.substr(start, len)

20
Searching and String Classes
  • Find the First Occurrence of One String Within
    Another
  • Location MyString2.find("Wor")
  • Returns a position or 1 if not found
  • Find First Occurrence of a Character Within a
    String
  • Location MyString2.find('W')
  • Returns a position or 1 if not found
  • Returning a substring of Characters from the
    string
  • MyString1 MyString2.substr(0,5)

21
In Conclusion
  • You can continue to use both old and new style
    strings in the same program
  • Requires both a string and a string.h include
Write a Comment
User Comments (0)
About PowerShow.com