C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.

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

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
Chapter 10.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
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.
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 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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.
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.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 8 Arrays and Strings
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
COP 3530 Data Structures & Algorithms Discussion Session 3.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
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.
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.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC 270 – Survey of Programming Languages
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Chapter 15 Strings as Character Arrays
Sahar Mosleh California State University San MarcosPage 1 Character String.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Chapter 7 Continued Arrays & Strings. Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart.
1 Strings in C++ The string Class Definition of Strings How to declare strings in C++: the string class Operations on strings –Concatenation, comparison.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Chapter Characters, Strings, and the string class 10.
Strings, Slide Fundamental Programming 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.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Computer Programming BCT 1113
Object Oriented Programming COP3330 / CGS5409
C++ STRINGS string is part of the Standard C++ Library
Chapter 8: The string Type
Review for Final Exam.
Strings A collection of characters taken as a set:
Representation and Manipulation
Engineering Problem Solving with C++, Etter
Review for Final Exam.
Presentation transcript:

C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type

Objectives 2 Learn about C -strings Examine the use of string functions to process C -strings Discover how to input data into—and output data from—a C -string

string Type  To use the data type string, the program must include the header file string  The statement: string name = "William Jacob"; declares name to be a string variable and also initializes name to "William Jacob"  The first character, 'W', is in position 0  The second character, 'i', is in position 1  name is capable of storing any size string C++ Programming: Program Design Including Data Structures, Fifth Edition 3

string Type (cont'd.)  Binary operator + have been defined for the data type string  + performs the string concatenation operation  Example: str1 = "Sunny"; str2 = str1 + " Day"; stores "Sunny Day" into str2 4

Example 8-14: clear, empty, erase, length, AND size FUNCTIONS 5 Please check the textbook: chapter 8, page 462, Table 8-1 for other string functions // 0 // 1 strVar.erase(pos, n): deleting n chars from strVar starting at position pos.

Example 8-15: find FUNCTION 6 - strVar.find(str): return the index of the first occurrence of str in strVar. - strVar.find(str, pos): return the index of the first occurrence at or after pos where str is found in strVar. //

Example 8-16: insert AND replace FUNCTIONS 7 - strVar.insert(pos, str): insert all the characters of str at index pos into strVar. - strVar.insert(pos, n, str): insert n occurrences of the character str at index pos into strVar.

Example 8-17: substr FUNCTION 8 - strVar.substr(pos, len): returning a string that is a substring of strVar starting at pos

Example 8-18: swap FUNCTION 9 - strVar.swap(str): swaps the contents of strVar and str.

C- Strings (Character Arrays) 10  Character array: an array whose components are of type char  C -strings is an array of characters ending with the null- terminated ( '\0' )  Example:  'A' is the character A  "A" is the C -string A "A" represents two characters, 'A' and '\0‘  Char City1[] = “Dallas”; // C-string  Char City1[] = {‘D’, ‘a’, ‘l’, ‘l’, ‘a’, ‘s’}; //Char array

C- Strings (Character Arrays) (cont'd.) 11  Consider the statement char name[16] = “Hello”;  Since C -strings are null terminated and name has 16 components, the largest string that it can store has 15 characters  If you store a string of length, say 10 in name  The first 11 components of name are used and the last five are left unused

C- Strings (Character Arrays) (cont'd.) 12 The statement char name[16] = {‘J’,’o’,’h’,’n’,’\0’}; declares an array name of length 16 of type char and stores the C -string "John" in it The statement char name[] = "John"; declares an array name of length 5 and stores the C - string "John" in it. char name[16] = {‘J’,’o’,’h’,’n’,’\0’}; = char name[] = "John";

C- Strings (Character Arrays) (cont'd.) 13

String Comparison 14  C -strings are compared character by character using the collating sequence of the system  If we are using the ASCII character set  "Air" < "Boat"  "Air" < "An"  "Bill" < "Billy"  "Hello" < "hello"

String Comparison (cont’d.) 15 Example_9-6.cpp

Reading and Writing Strings 16 Most rules that apply to arrays apply to C -strings as well Aggregate operations, such as assignment and comparison, are not allowed on arrays The one place where C++ allows aggregate operations on arrays is the input and output of C - strings (that is, character arrays)

String Input 17  Assume we declare char name[31];  cin >> name; stores the next input C - string into name  To read strings with blanks, use get : cin.get(str, m+1);  Stores the next m characters into str but the newline character is not stored in str  If the input string has fewer than m characters, the reading stops at the newline character

String Output 18  cout << name; outputs the content of name on the screen  << continues to write the contents of name until it finds the null character  If name does not contain the null character, then we will see strange output << continues to output data from memory adjacent to name until '\0' is found

Class Activity 19  Consider the following statement: char str1[16], str2[16]; cin.get(str1,3); cin.get(str2,4); cout << str1; cout << str2;  What is the value of str1, str2 and final output for the following input: C++ Programming Answer: str1={‘C’,‘+’,‘\0’} str2={‘+’,‘ ’,‘P’,‘\0’} Final output: C++ P ‘ cin.get.cpp

Class Activity 20  Consider the following statement: char str1[16], str2[16], discard; cin.get(str1,17); cin.get(discard); cin.get(str2,4); cout << str1; cout << str2;  What is the value of str1, str2 and final output for the following input: C++ Programming Answer: str1={‘C’,‘+’,,‘+’,‘ ’,‘P’,‘r’,‘o’, ‘g’,‘r’,‘a’,‘m’,‘m’,‘i’,‘n’,‘g’,‘\0’} str2={‘C’,‘+’,‘+’,‘\0’} Final output: C++ ProgrammingC++ ‘

Arrays of Strings 21  Strings in C++ can be manipulated using either the data type string or character arrays ( C -strings)

Arrays of Strings and the string Type 22  To declare an array of 100 components of type string : string list[100];  Basic operations, such as assignment, comparison, and input/output, can be performed on values of the string type  The data in list can be processed just like any one-dimensional array

Arrays of Strings and C -Strings (Character Arrays) 23

Summary 24  In C++, C -strings are null terminated and are stored in character arrays  Commonly used C -string manipulation functions include:  strcpy, strcmp, and strlen