Auburn University http://www.eng.auburn.edu/~xqin COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 DOS vs. UNIX files Ending lines with “\r\n” vs. “\n” Reading an entire line at a time getline() To skip white space or not cin >> ch; vs. ch = cin.get();
Chapter 10.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
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
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
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.
Chapter 3: Input/Output
1 Character Strings (Cstrings) Reference: CS215 textbook pages
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students C-strings and strings.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
C/C++ Basics (VI) Strings and Vectors Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
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.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Enumerated Types and Strings Types.
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 3: Getting Started & Input / Output (I/O)
Topic 2 Input/Output.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Input/Output.
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Command Line Arguments
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Exercise 1 From Lec80b-Ponter.ppt.
Working with Strings Lecture 2 Hartmut Kaiser
Chapter 3: Expressions and Interactivity.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
COMP 2710 Software Construction Pointers
COMP 2710 Software Construction File I/O
Chapter 2 part #3 C++ Input / Output
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Learning Objectives String Class.
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.
Working with Strings Lecture 2 Hartmut Kaiser
Today’s Lecture I/O Streams Tools for File I/O
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
Wednesday 09/23/13.
String What it is Why it’s useful
Chapter 3 Input output.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 2 part #3 C++ Input / Output
Using string type variables
Chapter 1 c++ structure C++ Input / Output
getline() function with companion ignore()
Presentation transcript:

Auburn University http://www.eng.auburn.edu/~xqin COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu TBC=22 These slides are adapted from notes by Dr. Walter Savitch (UCSD)

C-String Output Can output with insertion operator, << As we’ve been doing already: cout << news << " Wow.\n"; Where news is a c-string variable Possible because << operator is overloaded for c-strings! Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Exercise 1 What C-string will be stored in myString? char myString[30] = “Hello”; strcat(myString, “ world!”);

Exercise 2 What is wrong with the following code? char courseString[] = “comp3000”; strcat(courseString, “ programming”);

Exercise 3 What is the output? char photo[30] = “beautiful”; Char myphoto[30]; strcpy(myphoto, photo); strcat(myphoto, “ picture!”); Cout << myphoto << endl;

C-String Input (demo: cstring.cpp) Can input with extraction operator, >> Issues exist, however Whitespace is "delimiter" Tab, space, line breaks are "skipped" Input reading "stops" at delimiter Watch size of c-string Must be large enough to hold entered string! C++ gives no warnings of such issues! cstring:.cpp in comp3000/demo

Exercise 4 What is the output? char a[80], b[80]; cout << "Enter input: "; cin >> a >> b; cout << a << b << "END OF OUTPUT\n"; Dialogue offered: Enter input: Do be do to you! DobeEND OF OUTPUT Note: Underlined portion typed at keyboard C-string a receives: “Do" C-string b receives: "be"

C-String Line Input Demo: getline.cpp Can receive entire line into c-string Use getline(), a predefined member function: char a[80]; cout << "Enter input: "; cin.getline(a, 80); cout << a << "END OF OUTPUT\n"; Dialogue: Enter input: Do be do to you! Do be do to you!END OF INPUT Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

More getline() Can explicitly tell length to receive: char shortString[5]; cout << "Enter input: "; cin.getline(shortString, 5); cout << shortString << "END OF OUTPUT\n"; Results: Enter input: dobedowap dobeEND OF OUTPUT Forces FOUR characters only be read Recall need for null character! Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Example: Command Line Arguments Programs invoked from the command line (e.g. a UNIX shell, DOS command prompt) can be sent arguments Example: COPY C:\FOO.TXT D:\FOO2.TXT This runs the program named “COPY” and sends in two C-String parameters, “C:\FOO.TXT” and “D:\FOO2.TXT” It is up to the COPY program to process the inputs presented to it; i.e. actually copy the files Arguments are passed as an array of C-Strings to the main function

Example: Command Line Arguments Header for main int main(int argc, char *argv[]) argc specifies how many arguments are supplied. The name of the program counts, so argc will be at least 1. argv is an array of C-Strings. argv[0] holds the name of the program that is invoked argv[1] holds the name of the first parameter argv[2] holds the name of the second parameter Etc. Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Example: Command Line Arguments // Echo back the input arguments int main(int argc, char *argv[]) { for (int i=0; i<argc; i++) cout << "Argument " << i << " " << argv[i] << endl; } return 0; Sample Execution > Test hello world Argument 0 Test Argument 1 hello Argument 2 world Sample Execution > Test Argument 0 Test Invoking Test from command prompt Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Character I/O Input and output data ALL treated as character data e.g., number 10 outputted as "1" and "0" Conversion done automatically Uses low-level utilities Can use same low-level utilities ourselves as well Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Member Function get() Reads one char at a time Member function of cin object: char nextSymbol; cin.get(nextSymbol); Reads next char & puts in variable nextSymbol Argument must be char type Not "string"!

Member Function put() Outputs one character at a time Member function of cout object: Examples: cout.put("a"); Outputs letter "a" to screen char myString[10] = "Hello"; cout.put(myString[1]); Outputs letter "e" to screen Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

More Member Functions putback() peek() ignore() Once read, might need to "put back" cin.putback(lastChar); peek() Returns next char, but leaves it there peekChar = cin.peek(); ignore() Skip input, up to designated character cin.ignore(1000, "\n"); Skips at most 1000 characters until "\n" Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Character-Manipulating Functions: Display 9.3 Some Functions in <cctype> (1 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Character-Manipulating Functions: Display 9.3 Some Functions in <cctype> (2 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Character-Manipulating Functions: Display 9.3 Some Functions in <cctype> (3 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Standard Class string Defined in library: #include <string> using namespace std; String variables and expressions Treated much like simple types Can assign, compare, add: string s1, s2, s3; s3 = s1 + s2; //Concatenation s3 = "Hello Mom!" //Assignment Note c-string "Hello Mom!" automatically converted to string type! Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Display 9.4 Program Using the Class string Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

I/O with Class string Just like other types! string s1, s2; cin >> s1; cin >> s2; Results: User types in: May the hair on your toes grow long and curly! Extraction still ignores whitespace: s1 receives value "May" s2 receives value "the" Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

getline() with Class string For complete lines: string line; cout << "Enter a line of input: "; getline(cin, line); cout << line << "END OF OUTPUT"; Dialogue produced: Enter a line of input: Do be do to you! Do be do to you!END OF INPUT Similar to c-string’s usage of getline() Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Other getline() Versions Can specify "delimiter" character: string line; cout << "Enter input: "; getline(cin, line, "?"); Receives input until "?" encountered getline() actually returns reference string s1, s2; getline(cin, s1) >> s2; Results in: (cin) >> s2; Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Pitfall: Mixing Input Methods Be careful mixing cin >> var and getline int n; string line; cin >> n; getline(cin, line); If input is: 42 Hello hitchhiker. Variable n set to 42 line set to empty string! cin >> n skipped leading whitespace, leaving "\n" on stream for getline()! Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Class string Processing Same operations available as c-strings And more! Over 100 members of standard string class Some member functions: .length() Returns length of string variable .at(i) Returns reference to char at position i Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Display 9.7 Member Functions of the Standard Class string (1 of 2) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Display 9.7 Member Functions of the Standard Class string (2 of 2) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

C-string and string Object Conversions Automatic type conversions From c-string to string object: char aCString[] = "My C-string"; string stringVar; stringVar = aCstring; Perfectly legal and appropriate! aCString = stringVar; ILLEGAL! Cannot auto-convert to c-string Must use explicit conversion: strcpy(aCString, stringVar.c_str()); Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Summary C-string variable is "array of characters" With addition of null character, "\0" C-strings act like arrays Cannot assign, compare like simple variables Libraries <cctype> & <string> have useful manipulating functions cin.get() reads next single character getline() versions allow full line reading Class string objects are better-behaved than c-strings Copyright © 2010 Pearson Addison-Wesley. All rights reserved.