C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
1 Week 1304/07/2005Course ISM3230Dr. Simon Qiu  Learn how to create and manipulate enumeration type  Become aware of the typedef statement  Learn about.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 9: Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 9: Arrays and Strings
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
C++ Programming:. Program Design Including
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
Numeric Types, Expressions, and Output ROBERT REAVES.
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
Chapter 6: Arrays: Lists and Tables
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
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++
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
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.
String Class Mohamed Shehata 1020: Introduction to Programming.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
CS Computer Science IA: Procedural Programming
Class string and String Stream Processing: A Deeper Look
Engineering 1020: Introduction to Programming Fall 2018
String Messy Details Unsigned vs signed.
C++ STRINGS string is part of the Standard C++ Library
Chapter 8: The string Type
10.1 Character Testing.
Chapter 3: Input/Output
Class string and String Stream Processing
Representation and Manipulation
Engineering Problem Solving with C++, Etter
Microsoft Visual Basic 2005: Reloaded Second Edition
Additional string operators
character manipulation
Presentation transcript:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition The string Type To use the data type string, the program must include the header file The statement: string name = "William Jacob"; declares name to be a string variable and also initializes name to "William Jacob" The first character, 'W', in name is in position 0; the second character, 'i', is in position 1, and so on

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition The string Type (continued) The variable name is capable of storing any size string Binary operator + (to allow the string concatenation operation), and the array subscript operator [], have been defined for the data type string For example, If str1 = "Sunny", the statement stores the string "Sunny Day" into str2 : str2 = str1 + " Day";

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition Input/Output and the string Type The function getline −Reads until end of the current line −string s; −getline(cin, s); //accept spaces −cout<<s<<endl;

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition length Function Length returns the number of characters currently in the string The syntax to call the length function is: strVar.length() where strVar is variable of the type string length has no arguments length returns an unsigned integer The value returned can be stored in an integer variable

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition size Function The function size is same as the function length Both functions return the same value The syntax to call the function size is: strVar.size() where strVar is variable of the type string As in the case of the function length, the function size has no arguments

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition Example 8-14: clear, empty, erase, length, AND size FUNCTIONS 7 The length function return a value of size_type data type.

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition find Function find searches a string for the first occurrence of a particular substring Returns an unsigned integer value of type string::size_type giving the result of the search The syntax to call the function find is: strVar.find(strExp) where strVar is a string variable and strExp is a string expression evaluating to a string The string expression, strExp, can also be a character

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition find Function (continued) If successful, find returns the position in strVar where the match begins For the search to be successful the match must be exact If unsuccessful, find returns the special value string::npos (“not a position within the string”)

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition

substr Function substr returns a particular substring of a string The syntax to call the function substr is: strVar.substr(expr1,expr2) where expr1 and expr2 are expressions evaluating to unsigned integers

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition substr Function (continued) The expression expr1 specifies a position within the string (starting position of the substring) The expression expr2 specifies the length of the substring to be returned

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition

swap Function swap interchanges the contents of two string variables The syntax to use the function swap is strVar1.swap(strVar2); where strVar1 and strVar2 are string variables Suppose you have the following statements: string str1 = "Warm"; string str2 = "Cold"; After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "War"

Prepared by: Malak Abdullah C++ Programming: From Problem Analysis to Program Design, Fourth Edition Reading from files