what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Similar documents
Lecture 3 The character, string data Types Files

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

ENGI 1020 Introduction to Computer Programming R E Z A S H A H I D I J U L Y 2 6,

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

Computer Programming

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Sixth lecture; classes, objects, reference operator.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CS242 COMPUTER PROGRAMMING

Objectives. In this chapter, you will:

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Lab # 02. Basic Elements of C++ _ Part1

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Ch 6-8. C-Strings. April 30, Prof. Young-Tak Kim

Software Design & Programming I

2 nd Week Lecture Notes

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Name Section: M/W T/TH Number Definition Matching (8 Points)

1. In C++, reserved words are the same as predefined identifiers. a. True

CSCE 121 ENGR 112 List of Topics for Exam 1

Copyright 2003 Pearson Education, Inc. Slide 1

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

IT 374 C# and Applications/ IT695 C# Data Structures

Name Section: M/W T/TH Number Definition Matching (6 Points)

Practice test for midterm 1

CS103L SPRING 2018 UNIT 7: FILE I/O

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

Unit 2: The string class

Definition Matching (10 Points)

Exercise 1.1 Hello world

Unit 14. Passing Arrays & C++ Strings

C++ Input/Output Chapter 4 Topics

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Ch. 10, The C++ string Class

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

A First Program - Greeting.cpp

CS1 Lecture 3 Jan. 22, 2018

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

String Objects: The string class library

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Programming Abstractions

Introduction to Lexical Analysis

System Design and Programming II

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Computer Programming : C++

Engineering Problem Solving with C++, Etter

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Week 3: File I/O and Formatting 3.7 Formatting Output

CHAPTER 3 Expressions, Functions, Output

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CS 103 Unit 14 - Streams

Week 2. Muhao Chen.

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

Help Topic: MOOS-IvP String Utilities

Lab: Supplying Inputs to Programs

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

C++ basics Getting started with, and Data Types.

Getting started with C++ (Part 2)

CS11 Advanced C++ Lecture 2 Fall

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

Chapter 3: Input/Output

Language Reference Manual

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Exercise: Inventing Language

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Lecture 12. We have already used strings. Strings. Hello Class is a string.

C++ Input/Output: Streams

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS1 Lecture 3 Jan. 18, 2019

Chapter 3 - Notes Input/Output

Introduction to Programming using C++

Chapter 2 Basic Elements of C++

Integer Data Types. Data Type. Data Types. int, short int, long int

Transcription:

today: strings what are strings what are strings and why to use them reading: textbook chapter 8 a string in C++ is one of a special kind of data type called a class we will talk more about classes in detail at the end of the term but note that we have already used two classes when we covered files: the ifstream class and the ofstream class a class is a compound data type, unlike the simple, native data types we ve already discussed (e.g., int, char, bool, double, etc) a class has members: it has data fields and functions cis1.5-spring2008-azhar-leciv.2 1 cis1.5-spring2008-azhar-leciv.2 2 strings: declaring and initializing strings: output strings are declared like this: string s where s is a variable whose data type is a string you can set the value of the string using the assignment operator and double quotes ("): s = "hello"; NOTE that you use single quotes for char values and double quotes for string values: char c = A ; string s = "hello"; ALSO NOTE that when you use the string class, you also need to include the string header file: we have already used strings for output, e.g.: cout << "hello" << endl; but we have not yet output variables that were declared as strings but we can... it s just like this: string s = "hello"; cout << s << endl; cis1.5-spring2008-azhar-leciv.2 3 cis1.5-spring2008-azhar-leciv.2 4

strings: input there are two ways to read input values from the keyboard (or from a file) into a string variable: (1) using the >> operator (2) using the getline() function the first way, using the >> operator, will only read until the first whitespace character is read (the term whitespace refers to characters like blank spaces, tabs and newlines) when reading a string variable using the >> operator, the input will stop as soon as the first whitespace character is read example (on the next page): cout << "please enter your name:"; cin >> s; cout << "s = " << s << endl; if the user enters david ortiz when the program asks please enter your name: then the value of s will be david cis1.5-spring2008-azhar-leciv.2 5 cis1.5-spring2008-azhar-leciv.2 6 HOWEVER, when reading a string variable using the getline() function, the input will stop as soon as the first newline character is read (i.e., the user hits the ENTER key on the keyboard), e.g.: cout << "please enter your name:"; getline( cin, s ); cout << "s = " << s << endl; here, if the user enters david ortiz when the program asks please enter your name: then the value of s will be david ortiz note that the getline() function can optionally take a third argument, a delimiter this would be for cases where you wanted to stop the input not at whitespace or at a newline, but at some other character called the delimiter suppose you wanted to enter several commands for a robot to follow using one string, like this: forward wait backward turn left stop you could do this using the (vertical bar) character as the delimiter and the getline() function with three arguments, e.g.: cout << "enter some commands: "; getline( cin, s, ); If the user entered forward backward when prompted by the above program, the value of s would be forward, since the input would stop at the delimiter ( ). You would have to call getline( cin, s, ); again to read the next command (e.g., wait). cis1.5-spring2008-azhar-leciv.2 7 cis1.5-spring2008-azhar-leciv.2 8

strings: operators there are several operators that work with strings the plus sign (+) is the concatenation operator, e.g.: s1 = "david "; s2 = "ortiz"; s3 = s1 + s2; After the above code fragment, the value of s3 will be david ortiz the comparison operators also work with strings (==, <, <=, >, >=) the double equals sign (==) compares the value of two strings and returns true if they are the same, e.g.: bool a1, a2; s1 = "david "; s2 = "ortiz"; s3 = "david "; a1 = ( s1 == s2 ); a2 = ( s1 == s3 ); the value of a1 will be false and the value of a2 will be true cis1.5-spring2008-azhar-leciv.2 9 cis1.5-spring2008-azhar-leciv.2 10 the inequality operators (<, <=, >, >=) perform a lexical comparison between two strings a lexical comparison is like checking if two strings are in alphabetical order: one is less than the other if it comes before the other alphabetically EXCEPT, the lexical comparison is case sensitive and uses the ASCII table, which means that all the upper case letters (A..Z) come before (are less than) all the lower case letters (a..z), e.g.: bool a1, a2; s1 = "ABC"; s2 = "DEF"; s3 = "abc "; a1 = ( s1 < s2 ); a2 = ( s3 < s2 ); the value of a1 will be true because "ABC" < "DEF" and the value of a2 will be false because "abc" > "DEF" a string is like an array of char strings: indexes so you can use the index of the individual characters of the string just like you can use the indexes of the individual elements of an array, like the arrays of ints you created for the last homework assignment if you have: then: s[0] is assigned the value o (the letter oh ) s[1] is assigned the value r s[2] is assigned the value t s[3] is assigned the value i s[4] is assigned the value z you can also use the member function at() to find the value of an individual character of a string e.g., instead of using s[3], you can use s.at(3) cis1.5-spring2008-azhar-leciv.2 11 cis1.5-spring2008-azhar-leciv.2 12

strings: length strings: searching if you have: then the length of the string is 5 there are two member functions of the string class that will tell you the length of a string: length() and size() (they do the same thing) you call them like this: string s1; int n1, n2; s1 = "ortiz"; n1 = s1.length(); n2 = s1.size(); After this code fragment, the value of n1 will be 5 and so will the value of n2 the find() member function is used to locate a substring within a primary string the function returns the value of the index in the primary string at which the substring starts, if the substring exists in the primary string; or else the function returns the constant string::npos for example: string s1 = "david ortiz"; int n1, n2; n1 = s1.find( "avid", 0 ); n2 = s1.find( "ask", 0 ); the value of n1 will be 1 the value of n2 will be string::npos the first argument to the find() function is the substring to search for the second argument to the find() function is the index in the primary string at which to start searching; 0 means to start searching at the beginning of the primary string cis1.5-spring2008-azhar-leciv.2 13 cis1.5-spring2008-azhar-leciv.2 14 strings: editing there are three editing member functions that are part of the string class: insert() replace() erase() the insert() function inserts a substring into the primary string the replace() function replaces a substring with another substring within the primary string the erase() function erases a number of characters within the primary string example (on the next page): cout << "first, s=" << s << endl; s.insert( 0, "david " ); cout << "second, s=" << s << endl; s.replace( 0, 1, "D" ); s.replace( 6, 1, "O" ); cout << "third, s=" << s << endl; s.erase( 1, 4 ); cout << "fourth, s=" << s << endl; } // end main() The output of the above program will be: first, s=ortiz second, s=david ortiz third, s=david Ortiz fourth, s=d Ortiz cis1.5-spring2008-azhar-leciv.2 15 cis1.5-spring2008-azhar-leciv.2 16

strings: parsing the substr() member function is used to extract a substring from within a primary string example: string s1 = "D Ortiz"; string s2; cout << "s1=" << s1 << endl; s2 = s1.substr( 2, 5 ); cout << "s2=" << s2 << endl; } // end main() The output of the above program will be: s1=d Ortiz s2=ortiz cis1.5-spring2008-azhar-leciv.2 17