More about strings in C++

24
More about strings in C++

description

More about strings in C++. String member functions. The next three slides present information about functions that are members of the C++ string class (found in the cstring library) - PowerPoint PPT Presentation

Transcript of More about strings in C++

Page 1: More about strings in C++

More about strings in C++

Page 2: More about strings in C++

String member functions

• The next three slides present information about functions that are members of the C++ string class (found in the cstring library)

• Member functions in C++ work much like instance methods in Java; you must have a string object in order to call them

• You don’t have to call a string constructor in C++ (this is probably part of why you don’t really have to in Java either)

Page 3: More about strings in C++

3

size Function• Function size returns an unsigned int

value that equals the number of characters currently in the string

• Function length returns the same value as function size

• You must use dot notation in the call to function length or size

Page 4: More about strings in C++

4

find Function• 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 string::npos

Page 5: More about strings in C++

5

substr Function

• Function substr returns a particular substring of a string

• The first argument is an unsigned integer that specifies a starting position within the string

• The second argument is an unsigned integer that specifies the lengthlength of the desired substring (not the same as Java’s crazy substring method!)

• Positions of characters within a string are numbered starting from 0, not from 1

Page 6: More about strings in C++

String input

• There are several ways to read string data in C++; I’m just going to name 3:– The extraction operator (>>) is analogous to a Java

Scanner object’s next() method – that is, it will read a string up to the first delimiting character

– The get () function (member function of cin) reads one character at a time – can be used in a loop context to read string data

– The getline() function reads a string, sort of like Java Scanner’s nextLine() method – see next slide for syntax

Page 7: More about strings in C++

Functions getline() and ignore()

• The getline() function takes two arguments:– an input stream object (like cin)– a string– the function reads string data from the input

stream into the string

• The ignore() function (a member function of cin) can be used to get rid of unwanted data in the input stream

Page 8: More about strings in C++

8

Reading char data with the get() function

• An alternative to the >> operator is the get() function, a member of the istream class

• Like all member functions, get() can only be called by an object of its class type – a familiar instance of this class is cin

• The get() function reads and stores the next character in the input stream, skipping nothing – in other words, it reads white space

Page 9: More about strings in C++

9

Example

#include <iostream.h>#include <stdlib.h>

int main(){

char c;cout << “Enter a character: ”;cin.get(c); // reads the character enteredcout << “You entered: ” << c << endl;system(“PAUSE”);return 0;

}

Page 10: More about strings in C++

10

Weakness of get()

• In the previous example, a single character is read and echoed to the screen

• The next example attempts to do the same thing, only with two characters, but it doesn’t work

• Take a look at the code – what is output?

Page 11: More about strings in C++

11

Example

#include <iostream.h>#include <stdlib.h>

int main(){

char c, d;cout << "Enter a character: ";cin.get(c); // reads the character entered

cout << "Enter another character: "; cin.get(d); cout << "You entered: " << c << " and " << d << endl;

system("PAUSE");return 0;

}

Page 12: More about strings in C++

12

char first ; char middle ; char last ;

cin.get ( first ) ; cin.get ( middle ) ; cin.get ( last ) ;

NOTE: The file reading marker is left pointing to the space after the ‘B’ in the input stream.

first middle last

At keyboard you type: A[space]B[space]C[Enter]

first middle last

‘A’ ‘ ’ ‘B’

Page 13: More about strings in C++

13

The ignore() function

• Because get() reads all characters, including white space, there is no way to avoid reading spaces, newlines, etc.

• This is fine if you want to read these characters, but it is problematic in many instances, as we have seen

• To remedy this situation, use the ignore() function to skip unwanted characters, as described on the next several slides

Page 14: More about strings in C++

14

The ignore() function

• Like get(), ignore() is a member function of the istream class

• A call to ignore from the cin object can take two forms; the simpler form is:cin.ignore();– this form means ignore the next character in the stream– it is useful to place a call like this after each input

statement (cin.get() or extraction) in a program that uses any calls to cin.get() – this will clean up any lingering newline characters in the input stream

Page 15: More about strings in C++

15

Revised example

#include <iostream.h>#include <stdlib.h>

int main(){

char c, d;cout << "Enter a character: ";cin.get(c); // reads the character enteredcin.ignore(); // throws away the newline character

cout << "Enter another character: "; cin.get(d);

cin.ignore(); cout << "You entered: " << c << " and " << d << endl;

system("PAUSE");return 0;

}

Page 16: More about strings in C++

16

Other forms of ignore()

• You can also choose to call ignore() with one or two argument(s); using this form, you can specify how many characters to ignore, and/or the last character in a series of characters to ignore

• The example on the next slide illustrates these two forms

Page 17: More about strings in C++

17

Example

char c, d, e;cout << "Enter a character: ";cin.get(c); // reads the character enteredcin.ignore(1); // discards the newline charactercout << "Enter another character: ";cin.get(d);cin.ignore(100, '\n'); // discards next 100 characters or

// next newline, whichever comes firstcout << "Enter one more character: ";cin.get(e);cin.ignore(); // discards newlinecout << "You entered: " << c << ", " << d << " and " << e << endl;

Page 18: More about strings in C++

18

EXAMPLE

string message ; cin >> message ; cout << message ;

HOWEVER . . .

String Input in C++

Input of a string is possible using the extraction operator >>.

Page 19: More about strings in C++

19

Extraction operator >>

• When using the extraction operator ( >> ) to read input characters into a string variable: – the >> operator skips any leading whitespace

characters such as blanks and newlines– it then reads successive characters into the

string, and stops at the first trailing whitespace character (which is not consumed, but remains waiting in the input stream)

Page 20: More about strings in C++

20

Example

string name;cout << “Enter your name: ”;cin >> name;cout << “You entered: ” << name << endl;

Output:

Enter your name: Cate ShellerYou entered: Cate

Page 21: More about strings in C++

21

getline( ) Function• Because the extraction operator stops reading at the

first trailing whitespace, >> cannot be used to input a string with blanks in it

• use getline function with 2 arguments to overcome this obstacle

• First argument is an input stream variable, and second argument is a string variable

EXAMPLE

string message ;getline (cin, message ) ;

Page 22: More about strings in C++

22

getline(instream, string)

• getline does not skip leading whitespace characters such as blanks and newlines

• getline reads successive characters (including blanks) into the string, and stops when it reaches the newline character ‘\n’

• the newline is consumed by getline, but is not stored into the string variable

Page 23: More about strings in C++

23

Example

string name;cout << “Enter your name: ”;getline(cin, name);cout << “You entered: ” << name << endl;

Output:

Enter your name: Cate ShellerYou entered: Cate Sheller

Page 24: More about strings in C++

String concatenation

• We can do string concatenation in C++, but it’s more restricted than in Java

• Basically, you can concatenate a string with another string or with a char (and that’s all)