Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

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

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

Computer Programming

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

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

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

Class string and String Stream Processing Pearson Education, Inc. All rights reserved.

Engineering Problem Solving with C++, Etter

Computer Programming

CSI33 Data Structures

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

Dodatak D Standardna string klasa

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

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

CPSC 427a: Object-Oriented Programming

Lecture 3 The character, string data Types Files

Unit 14. Passing Arrays & C++ Strings

String Objects: The string class library

COMP322 - Introduction to C++

C++ for Python Programmers

Programming with Haiku

Pointers II. Class 31

Ch. 10, The C++ string Class

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

Dynamic Data Structures

Practice test for midterm 1

CS 101 Computer Programming and Utilization. Lecture 5, More Numerical computing (Slides courtesy Prof Ranade)

EL2310 Scientific Programming

2. First Program Stuff

CS11 Advanced C++ Lecture 2 Fall

pointers + memory double x; string a; int x; main overhead int y; main overhead

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

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Homework #3 CS2255 Fall 2012

Templates and Vectors

CSCE 110 PROGRAMMING FUNDAMENTALS

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

STL Standard Template Library

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

BITG 1113: Function (Part 2) LECTURE 5

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Computer Programming. Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay

The Standard Template Library Classes

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

EL2310 Scientific Programming

COMSW Introduction to Computer Programming in C

Why Is Repetition Needed?

C++ Strings, Enums. Data Processing Course, I. Hrivnacova, IPN Orsay

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

CSE 100: C++ TEMPLATES AND ITERATORS

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Scientific Computing

Programming Abstractions

CSci 127: Introduction to Computer Science

Introduction to C++ Introduction to C++ 1

W3101: Programming Languages C++ Ramana Isukapalli

CS11 Advanced C++ Fall Lecture 1

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Computer Programming

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

List Iterator Implementation

G52CPP C++ Programming Lecture 17

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

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

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

STL: C++ Standard Library

AN OVERVIEW OF C++ 1

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session : C++ Standard Library The string Class Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Quick Recap of Relevant Topics Object-oriented programming with structures and classes All classes seen so far were custom designed by us Template classes and functions 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture C++ Standard Library Collection of very useful classes that come with all C++ distributions The string classs 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Acknowledgment Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

C++ Standard Library Some classes and functionalities very commonly used across a wide variety of applications string, vector, list, queue, stack, priority_queue, set, map, Instead of each user defining these separately, C++ provides implementations of these as part of every distribution C++ Standard Library Collection of commonly used classes and functions Part of C++ ISO Standard Uses the best algorithms, are extensively tested, uses good dynamic memory management, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

C++ Standard Library All features declared within std namespace Saying at the beginning of your program takes care of this Must include appropriate header file in program to access standard library features #include <vector> Complete library fairly large We ll study only a few features in lectures Handout contains list of other classes in library Strongly encouraged to use Standard Library classes in your programs Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

C++ Standard Library (To Be Studied) string class For storing and manipulating strings without worrying about internal representation Container classes Holder of a collection of objects of another class (generic type) Usually implemented as template classes vector class map class list class Many more interesting classes not covered in lectures Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

The string class For representing and manipulating strings Must use at start of program Large collection of member functions We ll see only a small subset Some non-member functions (e.g. operator+, getline) also defined and very useful Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

Simple Programming using string Class string s1 = Hello ; string s2 = world! ; string s3 = s2; cout << s1 << << s3 << endl; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

Reading Input Strings: cin string s1 = Please type in your name: ; string s2; cout << s1; cin >> s2; cout << Your name is: << s2 << endl; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Reading Input Strings: getline string s1 = Please type in your name: ; string s2; cout << s1; getline(cin, s2); cout << Your name is: << s2 << endl; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

Concatenating strings: operator+ string s1 = Hello ; string s2 = world ; string s3 = s1 + s2 +!!! ; cout << s3 << endl; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

Concatenating strings: append string s; string s1 = Hello ; string s2 = my world!!! ; s.append(s1); s.append(s2, 4, 5); s.append(!!! ); cout << s << endl; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

Accessing Characters: operator[] string s1 = Hello world!!! ; if ((s1*6+ == ) && (s1*7+ == )), s1*6+ = m ; s1*7+ = y ; cout << s1; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

Accessing Characters: at string s1 = Hello world!!! ; if ((s1.at(6) == ) && (s1.at(7) == )), s1.at(6) = m ; s1.at(7) = y ; cout << s1; s1[100] vs s1.at(100): Illegal memory/garbage access vs out_of_range exception Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Finding a Substring: find and rfind string s1 = Hello world Hello!!! ; int i = s1.find( Hello ); int j = s1.find( Hi ); cout << i <<, << j << endl; string s1 = Hello world Hello!!! ; int i = s1.rfind( Hello ); int j = s1.rfind( Hi ); cout << i <<, << j << endl; string::npos A value that can never be a valid index Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Extracting Substrings: substr string s1 = Hello world Hello!!! ; cout << s1.substr(6) << << s1.substr(0, 5) << endl; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

C++ Iterator An object that points to an element in a collection of elements, and can be used to iterate through the elements in the collection Like a pointer, but not exactly the same Must support ++ (increment) and * (dereference) operations Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

Iterator Related Functions in string Class string s1 = Hi there! ; begin(), end() member functions for (string::iterator it = s1.begin(); it!= s1.end(); it++) { cout << *it; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

Iterator Related Functions in string Class string s1 = Hi there! ; rbegin(), rend() member functions for (string::reverse_iterator rit= s1.rbegin(); rit!= s1.rend(); rit++) { cout << *rit; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20

Summary C++ Standard Library string class and its usage Many more member functions exist Encouraged to read them up on your own Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 21