1 Principles of Computer Science I Honors Section Note Set 10 CSE 1341.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
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.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
Strings in C++ 04/29/11. Quiz Arrays  Section 7.1 & 7.2  Array basics  Partially filled arrays  Passing individual elements Mon. 05/02/11.
Chapter 10.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
CSCE 121: Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.
1 Chapter 10 Characters, Strings, and the string class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
February 14, 2005 Characters, Strings and the String Class.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
STARTING OUT WITH STARTING OUT WITH Class 9 Honors.
CSC 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ Basics #7 Basic Input and Output. In this video Standard output (cout) Standard input (cin) stringstream.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
1 Principles of Computer Science I Honors Section Note Set 9 CSE 1341.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Chapter 15 Strings as Character Arrays
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Programming Fundamentals. Today’s Lecture Multidimensional Arrays Arrays as Class Member Data Arrays of Objects C-Strings.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter Characters, Strings, and the string class 10.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Characters and Strings
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Characters, C-Strings, and More About the string Class
C++ Strings References: :" iomanip, sstream.
Standard Version of Starting Out with C++, 4th Edition
Chapter 2 part #3 C++ Input / Output
Standard Version of Starting Out with C++, 4th Edition
Chapter 2 part #3 C++ Input / Output
C++ Programming Lecture 20 Strings
Strings Skill Area 313 Part C
Today’s Objectives 28-Jun-2006 Announcements
getline() function with companion ignore()
Presentation transcript:

1 Principles of Computer Science I Honors Section Note Set 10 CSE 1341

2 Today String Streams

3 What’s the difference between: 255 and “255” ??????????

4 Background VAB Web Cam pictures Rover Communcations Mission Control (YOU)

5 Background Communication in character strings –s –Sent to the robot as a string of characters Problem: –You have to build the string of characters before you can send it to the robot –You have to interpret the string of characters from the VAB worksheet before you can use them in calculations

6 Numbers to Strings Difficult/annoying with c-strings (null terminated char arrays) Difficult to append a number to a c-string or stl-string //Can’t Do this string s; s = 32; cout << s; //WILL NOT display 32a //Can’t Do this either char str[25]; strcpy(str, 32); //WILL NOT COMPILE cout << s;

7 c-way or old way atoi(const char*), atof(const char*) –will convert an ascii string of digits to an integer –have to worry about c-string being properly formatted –What if result is too big to fit in an integer??

8 Remember cout??? You can send any fundamental data type to cout //This is OK! cout << 32; cout << “32”; cout << “go forward”; cout << 3.456; Can we do something similar, but store the data in a string instead of sending it to the screen?

9 String Stream string stream objects handle (behind the scenes) conversion of data types Can insert or extract information from a string stream using >> and << Stored in a String behind the scenes stringstream ss; istringstream iss; ostringstream oss;

10 Example #include int main() { stringstream s; int x = 25; double y = 2.34; s << x; s << “ “; s << y; string str; s >> str; //like cin, but source is s cout << str; //What will this print??? return 0; }

11 Example stringstream ss; ss << “ ”; int temp; for (int i = 0; i < 6; i++) { ss >> temp; cout << temp * 2; }

Example stringstream ss; char str[] = “ ”; ss << str; int temp; for (int i = 0; i < 6; i++) { ss >> temp; cout << temp * 2; }

13 Functions str() –return a stl-string copy of what’s in the stringstream getline(…) –used as if you were getting a line from cin

14 Example stringstream ss; string temp; ss << "C++ is the best ever!"; ss >> temp; cout << temp; cout << endl; getline(ss, temp); cout << temp; C++ is the best ever!

15 Questions??? ?