240 likes | 397 Views
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)
E N D
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)
size Function • Function size returns an unsigned int value that equals the number of characters currently in the string • Functionlengthreturns the same value as function size • You must use dot notation in the call to function lengthor size
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 stringexpression, or a char value • If the substring was not found, function findreturns the special value string::npos
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 length of the desired substring (not the same as Java’s crazy substring method!) • Positionsof characters within a string arenumbered starting from 0, not from 1
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
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
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
Example #include <iostream.h> #include <stdlib.h> int main() { char c; cout << “Enter a character: ”; cin.get(c); // reads the character entered cout << “You entered: ” << c << endl; system(“PAUSE”); return 0; }
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?
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; }
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. At keyboard you type:A[space]B[space]C[Enter] first middle last ‘A’ ‘ ’ ‘B’ first middle last
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
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
Revised example #include <iostream.h> #include <stdlib.h> int main() { char c, d; cout << "Enter a character: "; cin.get(c); // reads the character entered cin.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; }
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
Example char c, d, e; cout << "Enter a character: "; cin.get(c); // reads the character entered cin.ignore(1); // discards the newline character cout << "Enter another character: "; cin.get(d); cin.ignore(100, '\n'); // discards next 100 characters or // next newline, whichever comes first cout << "Enter one more character: "; cin.get(e); cin.ignore(); // discards newline cout << "You entered: " << c << ", " << d << " and " << e << endl;
String Input in C++ Input of a string is possible using the extraction operator >>. EXAMPLE string message ; cin >> message ; cout << message ; HOWEVER . . .
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)
Example string name; cout << “Enter your name: ”; cin >> name; cout << “You entered: ” << name << endl; Output: Enter your name: Cate Sheller You entered: Cate
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 ) ;
getline(instream, string) • getline does notskip 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
Example string name; cout << “Enter your name: ”; getline(cin, name); cout << “You entered: ” << name << endl; Output: Enter your name: Cate Sheller You entered: Cate Sheller
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)