More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

Strings.
Classes and Data Abstraction Lecture 9. Objects Models of things in the real world Defined in classes  Class name is the object name Example: Library.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
File streams Chapter , ,
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 3: Input/Output
C++ Vs. Java Who will win?. Look Ma, no classes! C++ was conceived as an object-oriented extension to C C was (is) purely procedural language –Functions.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
1 Chapter 4 Program Input and the Software Design Process.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
No I/O is built into C++ l instead, a library provides input stream and output stream KeyboardScreen executing program istreamostream 1.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 8 Strings and Vectors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Program Input and the Software Design Process. 2 Chapter 4 Topics  Input Statements to Read Values for a Program using >>, and functions get, ignore,
Chapter 3: Input/Output
1 Character Strings (Cstrings) Reference: CS215 textbook pages
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 4 Program Input and the Software Design Process.
Chapter 15 Strings as Character Arrays
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Program Input and the Software Design Process ROBERT REAVES.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
Chapter 8 Strings and Vectors. Slide 8- 2 Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Chapter 4 Program Input and the Software Design Process.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CS221 C++ Basics. C++ Data Types structured array struct union class address pointer reference simple integral char short int long bool floating float.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Lecture 24: 12/3/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 4 Program Input and the Software Design Process
Topic 2 Input/Output.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Chapter 13 Applied Arrays: Lists and Strings
Basic Input and Output Operations
getline() function with companion ignore()
Input and Output Chapter 3.
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Introduction to C++ Programming
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3: Input/Output
Introduction to C++ Programming
Chapter 4 Program Input and the Software Design Process
Using string type variables
Dale/Weems/Headington Program Input and the Software Design Process
getline() function with companion ignore()
Presentation transcript:

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)

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

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

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 length The second argument is an unsigned integer that specifies the length 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

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

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

9 Example #include int main() { char c; cout << “Enter a character: ”; cin.get(c);// reads the character entered cout << “You entered: ” << c << endl; system(“PAUSE”); return 0; }

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?

11 Example #include 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; }

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. firstmiddlelast At keyboard you type: A [ space]B [ space ] C [ Enter] firstmiddlelast ‘A’‘ ’‘B’

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

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

15 Revised example #include 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; }

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

17 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;

18 EXAMPLE string message ; cin >> message ; cout << message ; HOWEVER... String Input in C++ Input of a string is possible using the extraction operator >>.

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)

20 Example string name; cout << “Enter your name: ”; cin >> name; cout << “You entered: ” << name << endl; Output: Enter your name: Cate Sheller You entered: Cate

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 ) ;

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

23 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)