Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings

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

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.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects.
Chapter 3: Input/Output
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Lecture 5: Part 2 Classes and Data Abstraction. Objects Models of things in the real world Defined in classes  Class name is the object name Example:
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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:
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Chapter 3: Input/Output
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.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
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.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 3 Selection Statements
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
CMSC 202 Lesson 2 C++ Primer.
Operator Overloading; String and Array Objects
ECE Application Programming
ECE Application Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Standard Input/Output Streams
Operator Overloading; String and Array Objects
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.
Operator Overloading; String and Array Objects
10.1 Character Testing.
Chapter 5 Input and Output Streams
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.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 2 part #3 C++ Input / Output
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Algorithmic complexity
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
Today’s Objectives 28-Jun-2006 Announcements
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Programming Fundamental-1
Presentation transcript:

Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings

Announcements/reminders Program 1 due Friday, 2/8 All programs to be submitted via Blackboard Submit a single .zip file containing all files for this assignment Additional instructor support for course Grader: Shubham Tikare Office hours: Tues 9-11 AM, Ball 301E (ECE Conf Rm) Tutor: Felipe Loera Hours available on CLASS site (link also on home page) 6/12/2019 Data Structures: Lecture 6

Data Structures: Lecture 6 Lecture outline Review Output manipulators Other input methods C++ strings 6/12/2019 Data Structures: Lecture 6

Review: Output manipulators setprecision: change number of digits displayed to the right of the decimal point fixed: fixed format (no scientific notation) showpoint: force decimal point (and trailing zeros) to be shown Default behavior (without showpoint) is to not show trailing zeros Default precision (with showpoint) is 6 Reset with noshowpoint 6/12/2019 Data Structures: Lecture 6

Clarifying manipulators showpoint does not imply fixed format Could be fixed point, could be scientific notation Scientific notation used if precision < # digits before decimal point Default behavior: precision = sig. figures After showpoint: precision = digits after point Clearing fixed flag: defaultfloat There aren’t just two options … … but we’re not going to discuss the other ones 6/12/2019 Data Structures: Lecture 6

Review: Characters and input Input (cin) streams Use cin to read values into variables E.g., cin >> x; Skips whitespace characters Input value must be compatible with type of x Reading n characters: cin.get(buffer, n); Reading 1 character: cin.get(ch); Reading an entire line (at most m characters): cin.getline(buffer, m) May need cin.ignore(x) to skip characters 6/12/2019 Data Structures: Lecture 6

Standard Library Class string Header <string>, namespace std Basic uses: Initialization: string s1( "hi" ); Input/output (as in cout << s1) Assignment: s1 = "hi"; Can also use: Relational operators: ==, !=, >=, >, <=, < Perform char-by-char comparison using ASCII values Concatenation: += E.g.: s1 += “lly”  s1 = “hilly” 6/12/2019 Data Structures: Lecture 6

Standard Library Class string (Cont.) Substring member function substr s1.substr( 0, 14 ); Starts at location 0, gets 14 characters s1.substr( 15 ); Substring beginning at location 15, to the end Overloaded [] Access one character No range checking (if subscript invalid) Member function at Accesses one character Example s1.at( 10 ); Has bounds checking, throws an exception if subscript is invalid Member function empty Returns true if no characters in string Member function length Returns number of characters in string string s = “Hello”;  s.length() = 5 6/12/2019 Data Structures: Lecture 6

Example: Strings & functions int main() { string s1( "happy" ); string s2( " birthday" ); string s3; // test overloaded equality and relational operators cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 << "\"; s3 is \"" << s3 << '\"' << "\n\nThe results of comparing s2 and s1:" << "\ns2 == s1 yields " << ( s2 == s1 ? "true" : "false" ) << "\ns2 != s1 yields " << ( s2 != s1 ? "true" : "false" ) << "\ns2 > s1 yields " << ( s2 > s1 ? "true" : "false" ) << "\ns2 < s1 yields " << ( s2 < s1 ? "true" : "false" ) << "\ns2 >= s1 yields " << ( s2 >= s1 ? "true" : "false" ) << "\ns2 <= s1 yields " << ( s2 <= s1 ? "true" : "false" ); 6/12/2019 Data Structures: Lecture 6

Data Structures: Lecture 6 Example (cont.) Output from previous slide: s1 is “happy”; s2 is “ birthday”; s3 is “” The results of comparing s1 and s2: s2 == s1 yields false s2 != s1 yields true s2 > s1 yields false s2 < s1 yields true s2 >= s1 yields false s2 <= s1 yields true 6/12/2019 Data Structures: Lecture 6

Data Structures: Lecture 6 Example (cont.) // test string member function empty cout << "\n\nTesting s3.empty():" << endl; if ( s3.empty() ) { cout << "s3 is empty; assigning s1 to s3;" << endl; s3 = s1; // assign s1 to s3 cout << "s3 is \"" << s3 << "\""; } // end if // test overloaded string concatenation operator cout << "\n\ns1 += s2 yields s1 = "; s1 += s2; // test overloaded concatenation cout << s1; // test concatenation operator with C-style string cout << "\n\ns1 += \" to you\" yields" << endl; s1 += " to you"; cout << "s1 = " << s1 << "\n\n"; 6/12/2019 Data Structures: Lecture 6

Data Structures: Lecture 6 Example (cont.) Output from previous slide: Testing s3.empty(): s3 is empty; assigning s1 to s3; s3 is “happy” s1 += s2 yields s1 = happy birthday s1 += “ to you” yields s1 = happy birthday to you 6/12/2019 Data Structures: Lecture 6

Data Structures: Lecture 6 Example (cont.) // test string member function substr cout << "The substring of s1 starting at location 0 for\n" << "14 characters, s1.substr(0, 14), is:\n" << s1.substr( 0, 14 ) << "\n\n"; // test substr "to-end-of-string" option cout << "The substring of s1 starting at\n" << "location 15, s1.substr(15), is:\n" << s1.substr( 15 ) << endl; // test using subscript operator to create lvalue s1[ 0 ] = 'H'; s1[ 6 ] = 'B'; cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " << s1 << "\n\n"; // test subscript out of range with string member function "at" cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl; s1.at( 30 ) = 'd'; // ERROR: subscript out of range return 0; } // end main 6/12/2019 Data Structures: Lecture 6

Data Structures: Lecture 6 Example (cont.) Output from previous slide: The substring of s1 starting at location 0 for 14 characters, s1.substr(0, 14), is: happy birthday The substring of s1 starting at location 15, s1.substr(15), is: to you s1 after s1[0] = ‘H’ and s1[6] = ‘B’ is: Happy Birthday to you Attempt to assign ‘d’ to s1.at(30) yields abnormal program completion 6/12/2019 Data Structures: Lecture 6

Data Structures: Lecture 6 Final notes Next time Algorithmic complexity intro Reminders: Program 1 due Friday, 2/8 All programs to be submitted via Blackboard Submit a single .zip file containing all files for this assignment Additional instructor support for course Grader: Shubham Tikare Office hours: Tues 9-11 AM, Ball 301E (ECE Conf Rm) Tutor: Felipe Loera Hours available on CLASS site (link also on home page) 6/12/2019 Data Structures: Lecture 6