String class and its objects

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

M ANIPULATING S TRINGS Resources [1] Object Oriented Programming with C++ (3 rd Edition) E Balagurusamy [2] Teach Yourself C++ (3 rd Edition) H Schildt.
Strings.
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.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 19 - Class string and Stream Processing Outline 19.1Introduction 19.2 string Assignment and.
Today’s Class Class string –The basics –Assignment –Concatenation –Compare & swap –Find –Conversion to C-style char * strings –Iterators.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Arrays and String Using array Initialized arrays of different.
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.
1 Chapter 10 Characters, Strings, and the string class.
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.
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
C++ String Class. Outline  String Initialization  Basic Operations  Comparisons  Substrings  Swapping Strings  String Size  Finding Strings and.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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.
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++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
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.
CSC 270 – Survey of Programming Languages
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
STL string class Programming Team 2/15/2006. string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char*
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Chapter Characters, Strings, and the string class 10.
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.
13. string Operations Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Accessing String Elements.
Strings.
Lecture Overview Linked List quiz Finish last class' topic:
2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I.
C-Strings We have already seen that a C-string is a null-terminated array of characters.
CS Computer Science IA: Procedural Programming
Class string and String Stream Processing: A Deeper Look
Characters, C-Strings, and More About the string Class
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
Chapter 7: Strings and Characters
C++ STRINGS string is part of the Standard C++ Library
מחרוזות-String בשפת C++ ישנו תפקיד מיוחד למערך מסוג char רצף של תווים הנמצאים במערך מסוג char המסתיימת בתו אפס (הכוונה לאפס ממש '0\' , ולא לתו '0')
Strings in C++.
Programming with ANSI C ++
Chapter 9 One-Dimensional Arrays
10.1 Character Testing.
Class string and String Stream Processing
Chapter 9 Objects and Classes
Representation and Manipulation
Engineering Problem Solving with C++, Etter
Standard Version of Starting Out with C++, 4th Edition
Today’s Objectives 28-Jun-2006 Announcements
Library in c++ ( as cmath , iostream , …… etc.)
character manipulation
Unit-2 Objects and Classes
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

String class and its objects

Introduction A string is a sequence of character. We have used null terminated <char> arrays (C- strings or C-style strings) to store and manipulate strings. ANSI C++ provides a class called string. We must include <string> in our program.

Available Operations Creating string objects. Reading string objects from keyboard. Displaying string objects to the screen. Finding a substring from a string. Modifying string objects. Adding string objects. Accessing characters in a string. Obtaining the size of string. And many more.

Commonly Used String Constructors // For creating an empty string. String(const char *str); // For creating a string object from a null-terminated string. String(const string &str); // For creating a string object from other string object.

Creating String Objects string s1, s3; // Using constructor with no arguments. string s2(“xyz”); // Using one-argument constructor. s1 = s2; // Assigning string objects s3 = “abc” + s2; // Concatenating strings cin >> s1; // Reading from keyboard (one word) cout << s2; // Display the content of s2 getline(cin, s1) // Reading from keyboard a line of text s3 += s1; // s3 = s3 + s1; s3 += “abc”; // s3 = s3 + “abc”;

Manipulating String Objects string s1(“12345”); string s2(“abcde”); s1.insert(4, s2); // s1 = 1234abcde5 s1.erase(4, 5); // s1 = 12345 s2.replace(1, 3, s1); // s2 = a12345e

Manipulating String Objects insert() erase() replace() append()

Relational Operations Operator Meaning == Equality != Inequality < Less than <= Less than or equal > Greater than >= Greater than or equal string s1(“ABC”); string s2(“XYZ”); int x = s1.compare(s2); x == 0 if s1 == s2 x > 0 if s1 > s2 x < 0 if s1 < s2

String Characteristics void display(string &str) { cout << “Size = ” << str.size() << endl; cout << “Length = ” << str.length() << endl; cout << “Capacity = ” << str.capacity() << endl; cout << “Max Size = ” << str.max_size() << endl; cout << “Empty: ” << (str.empty() ? “yes” : “no”) << endl; cout << endl << endl; }

String Characteristics Function Task size() Number of elements currently stored length() capacity() Total elements that can be stored max_size() Maximum size of a string object that a system can support emply() Return true or 1 if the string is empty otherwise returns false or 0 resize() Used to resize a string object (effects only size and length)

Accessing Characters in Strings Function Task at() For accessing individual characters substr() For retrieving a substring find() For finding a specific substring find_first_of() For finding the location of first occurrence of the specific character(s) find_last_of() [] operator For accessing individual character. Makes the string object to look like an array.

Comparing and Swapping There is another overloaded version of compare int compare(int start_1, int length_1, string s_2, int start_2, int length_2) string s1, s2; int x = s1.compare(0, 2, s2, 2, 2); s1.swap(s2) Exchanges the content of string s1 and s2