The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Similar documents
Minimal Spanning Trees

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

The Critical-Path Algorithm

Implementing Linked Lists

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Scope and Parameter Passing

The Class Construct Part 1

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Scope and Parameter Passing

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

The Decreasing-Time Algorithm

The Class Construct Part 2

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Friends and Unary Operators

Building the Abstract Syntax Trees

Sampling Distribution Examples Sections 15.4, 15.5

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Binary Tree Applications

Dynamic Allocation of Memory

Recursive Linked Lists

Fundamental Data Types

Dynamic Allocation of Memory

Recursive Descent Parsers

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

The Graphics Pipeline

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Scheduling and Digraphs

Density Curves Sections

The Pairwise-Comparison Method

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

The CYK Parsing Algorithm

Street-Routing Problems

The Traveling Salesman Problem Brute Force Method

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Nondeterministic Programming in C++

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Stacks and their Applications

Solving Recursive Sequences by Iteration

LR Parsing - Conflicts

MySQL Creating a Database Lecture 3

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Programming Languages

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Programming Languages

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Function Definition Syntax Tree

XQuery FLOWR Expressions Lecture 35

The Graphics Pipeline

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

The Projection Matrix

Inheritance: The Fundamental Functions

Magnification and Minification

Recognition of Tokens

Ambient and Diffuse Light

PHP Queries and HTML Forms Lecture 23

List Iterator Implementation

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

The Coefficient of Determination

The Standard Template Library Classes

Inheritance: The Fundamental Functions

Introduction to Compiler Design

Displaying Distributions - Quantitative Variables

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

The Plurality-with-Elimination Method

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Insertions, Deletions, and Updates

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

The Normal Distribution

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Binary Tree Implementation

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Binary Tree Implementation

The Traveling Salesman Problem Cheapest-Link Algorithm

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

JFlex Regular Expressions

The x86 Architecture

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Basic PHP Lecture 17

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

Introduction to Databases

Transcription:

The string Class Lecture 21 Sections 2.9, 3.9, 3.10 Robb T. Koether Hampden-Sydney College Wed, Oct 17, 2018 Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 1 / 18

1 The String Class String Class Member Functions 2 Assignment Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 2 / 18

Outline 1 The String Class String Class Member Functions 2 Assignment Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 3 / 18

String Declarations and Initialization Strings string s; string t = "Hello"; // Empty string // Initialized A string may simply be declared, or it may be declared and initialized by another string. If it is not explicitly initialized, then it is set to the empty string. Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 4 / 18

String Operators Assignment = Input and output >>, << Equality ==,!= Relational <, >, <=, >= Concatenation +, += Subscript [] Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 5 / 18

Member Functions object.member-function() A data type may have member functions. A member function is a function that can be invoked only by means of an object of that type. Typically, a member function acts on the object that invokes it. We have already used the string member function size() to get the size of a string. Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 6 / 18

Outline 1 The String Class String Class Member Functions 2 Assignment Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 7 / 18

String Member Functions String Member Functions int length(); int size(); char* c_str(); length(), size() Returns the number of characters in the string. c_str(); Returns the same string, but as a C-type null-terminated string. Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 8 / 18

C-type Strings A C-type string is a simple structure, different from the string class, which is considerably more complicated. The characters are stored in consecutive bytes of memory. A null character (ASCII 0) marks the end of the string. C-type strings are also called null-terminated strings. The string "Hello, World" would be stored as H e l l o, W o r l d \0 Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 9 / 18

C-type Strings File Input string path = "C:/Documents/My_file.txt"; ifstream fin(path.c_str()); Some library functions require C++ strings and others require C-type strings. Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 10 / 18

Example Example LetterCount.cpp Experiment by changing isalpha() to isdigit(), ispunct(), and iscntrl(). Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 11 / 18

String Member Functions String Member Functions int find(string str); void append(string str); find() Get the index of a specified string str as a substring of the invoking string; return -1 if the specified string cannot be found. append() Append a specified string str to the end of the invoking string. Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 12 / 18

Example Example FindString.cpp Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 13 / 18

String Member Functions String Member Functions void insert(int i, string str); string substr(int i, int j); insert() Insert a specified string str into the invoking string, beginning at a specified position i. substr() Get the substring of a specified length j, beginning in a specified position i. Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 14 / 18

String Member Functions String Member Functions void erase(int i, int j); void clear(); bool empty(); erase() Remove a specified number j of characters from the invoking string, beginning in a specified position i. clear() Remove all characters from the string. empty() Determine whether the string is empty. Return true if the string has length 0; false otherwise. Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 15 / 18

Example Example FindAndReplace.cpp What happens if we replace "a" with "an"? Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 16 / 18

Outline 1 The String Class String Class Member Functions 2 Assignment Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 17 / 18

Assignment Assignment Read Sections 2.9, 3.9, 3.10. Robb T. Koether (Hampden-Sydney College) The string Class Wed, Oct 17, 2018 18 / 18