Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

Programming Logic and Design Sixth Edition
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 8: Manipulating Strings
Chapter 9: Arrays and Strings
Introduction to Programming with C++ Fourth Edition
Chapter 8 Arrays and Strings
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
A First Book of ANSI C Fourth Edition
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 8 Arrays and Strings
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 2: Using Data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 8: Manipulating Strings
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
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.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
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.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 9: Value-Returning Functions
Chapter 7: User-Defined Functions II
An Introduction to Programming with C++ Sixth Edition
The Selection Structure
Chapter 7: Strings and Characters
Chapter 3 The DATA DIVISION.
Chapter 8: The string Type
CIS16 Application Development and Programming using Visual Basic.net
String Manipulation and More Controls
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Programming Logic and Design Fifth Edition, Comprehensive
C++ Programming Lecture 20 Strings
Chapter 5: The Selection Structure
Variables and Constants
Presentation transcript:

Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 2 Objectives Determine the number of characters contained in a string Remove characters from a string Access characters contained in a string Replace characters in a string Insert characters within a string

Introduction to Programming with C++, Fourth Edition 3 Objectives (continued) Search a string for another string Compare a portion of a string variable’s contents to another string Duplicate a character within a string variable Concatenate strings

Introduction to Programming with C++, Fourth Edition 4 Manipulating Strings Programs often need to manipulate (process) string data –Verify that an inventory part number begins with a specific letter –Determine whether the last three characters in an employee number are valid

Introduction to Programming with C++, Fourth Edition 5 Determining the Number of Characters Contained in a String length() function: determines the number of characters contained in a string Returns an unsigned integer; you need to use the int type cast when assigning the return value to an int variable string.length()

Introduction to Programming with C++, Fourth Edition 6 Syntax and Examples of the length() Function

Introduction to Programming with C++, Fourth Edition 7 Syntax and Examples of the length() Function (continued)

Introduction to Programming with C++, Fourth Edition 8 Removing Characters from a String An application may need to remove characters from an item of data entered by the user –Remove a dollar sign from the beginning of a sales amount –Remove a percent sign from the end of a tax rate Use the erase() function to remove one or more characters located anywhere in a string variable string.erase(subscript[, count])

Introduction to Programming with C++, Fourth Edition 9 Removing Characters from a String (continued) Each character in a string is assigned a unique number (subscript) that indicates the character’s position in the string (starting from 0) subscript is the subscript of the first character you want removed from the string count is the number of characters you want removed If you omit count, erase() removes all characters from subscript to the end of the string

Introduction to Programming with C++, Fourth Edition 10 Syntax and Examples of the erase() Function

Introduction to Programming with C++, Fourth Edition 11 Accessing Characters Contained in a String An application may need to access one or more characters contained in a string –Determine whether the letter “K” appears as the third character in a string –Display only the string’s first five characters Use the substr() function to access any number of characters contained in a string variable string.substr(subscript[, count])

Introduction to Programming with C++, Fourth Edition 12 Accessing Characters Contained in a String (continued) substr() function contains two arguments: –Subscript is the subscript of the first character you want to access in the string (starting from 0) –The count argument, which is optional, specifies the number of characters you want to access Returns a string with count number of characters, beginning with the character whose subscript is specified by subscript If you omit count, it returns all the characters from the subscript position through the end of the string

Introduction to Programming with C++, Fourth Edition 13 Syntax and Examples of the substr() Function

Introduction to Programming with C++, Fourth Edition 14 Replacing Characters in a String Use the replace() function to replace a sequence of characters in a string variable with another sequence of characters –Use the replace() function to replace area code “800” with area code “877” in a phone number string.replace(subscript, count, replacementString)

Introduction to Programming with C++, Fourth Edition 15 Replacing Characters in a String (continued) string is the name of a string variable that contains one or more characters you want to replace subscript specifies where to begin replacing characters in the string count indicates the number of characters to replace replacementString contains the string that will replace the characters in the string

Introduction to Programming with C++, Fourth Edition 16 Syntax and Examples of the Replace() Function

Introduction to Programming with C++, Fourth Edition 17 Inserting Characters within a String Use the insert() function to insert characters within a string variable –Insert an employee’s middle initial within his or her name –Insert parentheses around the area code in a phone number string.insert(subscript, insertString)

Introduction to Programming with C++, Fourth Edition 18 Inserting Characters within a String (continued) subscript specifies where in the string you want the insertString inserted (0 is the beginning) Returns a string with the appropriate characters inserted

Introduction to Programming with C++, Fourth Edition 19 Syntax and Examples of the insert() Function

Introduction to Programming with C++, Fourth Edition 20 Searching a String Use the find() function to search a string variable to determine whether it contains a specific sequence of characters –Determine whether the area code “312” appears in a phone number –Determine whether the street name “Elm Street” appears in an address string.find(searchString, subscript)

Introduction to Programming with C++, Fourth Edition 21 Searching a String (continued) string: the string variable you want to search searchString: the string you are searching for subscript: the starting position for the search Search for searchString in string, starting with the character in position subscript in the string If searchString is found in the string, return the beginning position of searchString in the string Return –1 if the searchString is not found

Introduction to Programming with C++, Fourth Edition 22 Syntax and Examples of the find() Function

Introduction to Programming with C++, Fourth Edition 23 Syntax and Examples of the find() Function (continued)

Introduction to Programming with C++, Fourth Edition 24 Comparing a Portion of a string Variable’s Contents to Another String You can use comparison operators with strings Sometimes you need to compare a portion of one string –Compare the last two characters in the employNum variable to “12” to determine if the employee works in the Accounting department, which has a department code of 12 Use the compare() function to compare a portion of a string variable’s contents to another string string1.compare(subscript, count, string2)

Introduction to Programming with C++, Fourth Edition 25 Comparing a Portion of a string Variable’s Contents to Another String (continued) string1 and string2 are strings to compare string1 is a string variable and string2 can be a string literal constant or a string variable subscript specifies where in string1 the comparison should begin count indicates the number of characters in string1 to compare to the characters in string2

Introduction to Programming with C++, Fourth Edition 26 Syntax and Examples of the compare() Function

Introduction to Programming with C++, Fourth Edition 27 Syntax and Examples of the compare() Function (continued)

Introduction to Programming with C++, Fourth Edition 28 Duplicating a Character within a string Variable Use the assign() function to duplicate one character a specified number of times, then assign the resulting string to a string variable string.assign(count, character)

Introduction to Programming with C++, Fourth Edition 29 Duplicating a Character within a string Variable (continued) string is the name of a string variable that will store the duplicated characters count is either a numeric literal constant or the name of a numeric variable –Indicates the number of times you want to duplicate the character specified in the function’s character argument character can be either a character literal constant or the name of a char variable

Introduction to Programming with C++, Fourth Edition 30 Syntax and Examples of the assign() Function

Introduction to Programming with C++, Fourth Edition 31 Concatenating Strings Connecting (or linking) strings together is called concatenating Use the concatenation operator (+) to concatenate strings

Introduction to Programming with C++, Fourth Edition 32 Examples of Using the Concatenation Operator

Introduction to Programming with C++, Fourth Edition 33 Summary Programs often need to manipulate (process) string data length() determines the number of characters contained in a string variable erase() removes characters in a string substr() accesses characters in a string replace() replaces a sequence of characters in a string

Introduction to Programming with C++, Fourth Edition 34 Summary (continued) insert() inserts characters in a string find() searches a string to determine whether it contains a specific sequence of characters compare() compares a portion of a string variable’s contents to another string assign() duplicates one character a specified number of times, then assigns the resulting string to a string variable Use + to concatenate (join) strings