Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

Strings.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and 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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
Chapter 10.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
CS Nov 2006 C-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.
C-strings and C++ string Class
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
1 Chapter 10 Characters, Strings, and the string class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
February 14, 2005 Characters, Strings and the String Class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
STARTING OUT WITH STARTING OUT WITH Class 9 Honors.
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.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
Strings, Pointers and Tools
Chapter Characters, Strings, and the string class 10.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Strings: C-strings vs. Strings as Objects
C-Strings We have already seen that a C-string is a null-terminated array of characters.
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
Object Oriented Programming COP3330 / CGS5409
Strings: C-strings vs. Strings as Objects
10.1 Character Testing.
CPS120: Introduction to Computer Science
Standard Version of Starting Out with C++, 4th Edition
Today’s Objectives 28-Jun-2006 Announcements
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics 12.1 C-Strings 12.2 Library Functions for Working with C-Strings 12.3 Conversions Between Numbers and Strings 12.4 Writing Your Own C-String Handling Functions 12.5 More About the C++ string Class 12.6 Creating Your Own String Class 12-2

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string "Hi there!" would be stored in memory as shown: 12-3 Hithe re! \0

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What is NULL? The null character is used to indicate the end of a string It can be specified as –the character '\0' –the int value 0 –the named constant NULL 12-4

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Representation of C-strings As a string literal " Hi There!" As a pointer to char char *p; As an array of characters char str[20]; All three representations are pointers to char 12-5

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Literals A string literal is stored as a null-terminated array of char Compiler uses the address of the first character of the array as the value of the string String literal is a pointer to char 12-6 h i \0 value of "hi" is the address of this array

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array of char An array of char can be defined and initialized to a C-string char str1[20] = "hi"; An array of char can be defined and later have a string copied into it using strcpy or cin.getline char str2[20], str3[20]; strcpy(str2, "hi"); cout << "Enter your name: "; cin.getline(str3, 20); 12-7

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Array of char The name of an array of char is used as a pointer to char Unlike a string literal, a C-string defined as an array can be referred to in other parts of the program by using the array name 12-8

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Pointer to char Defined as char *pStr; Does not itself allocate memory Useful in repeatedly referring to C- strings defined as a string literal pStr = "Hi there"; cout << pStr << " " << pStr; 12-9

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Pointer to char Pointer to char can also refer to C-strings defined as arrays of char char str[20] = "hi"; char *pStr = str; cout << pStr; // prints hi Can dynamically allocate memory to be used for C-string using new 12-10

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Library Functions for Working with C-Strings Require cstring header file Functions take one or more C-strings as arguments. Argument can be: –Name of an array of char –pointer to char –string literal 12-11

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Library Functions for Working with C-Strings int strlen(char *str) Returns length of a C-string: cout << strlen("hello"); Prints: 5 Note: This is the number of characters in the string, NOT the size of the array that contains it 12-12

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley strcat strcat(char *dest, char *source) Takes two C-strings as input. It adds the contents of the second string to the end of the first string: char str1[15] = "Good "; char str2[30] = "Morning!"; strcat(str1, str2); cout << str1; // prints: Good Morning! No automatic bounds checking: programmer must ensure that 1 st string has enough room for result 12-13

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley strcpy strcpy(char *dest, char *source) Copies a string from a source address to a destination address char name[15]; strcpy(name, "Deborah"); cout << name; // prints Deborah Again, no automatic bounds checking 12-14

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley strcmp int strcmp(char *str1, char*str2) Compares strings stored at two addresses to determine their relative alphabetic order: Returns a value: less than 0 if str1 precedes str2 equal to 0 if str1 equals str2 greater than 0 if str1 succeeds str

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley strcmp Often used to test for equality if(strcmp(str1, str2) == 0) cout << "equal"; else cout << "not equal"; Also used to determine ordering of C-strings in sorting applications Note: –Comparisons are case-sensitive: "Hi" != "hi" –C-strings cannot be compared using == (compares addresses of C-strings, not contents) 12-16

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley strstr char *strstr(char *str1,char *str2) Searches for the occurrence of str2 within str1. Returns a pointer to the occurrence of str2 within str1 if found, and returns NULL otherwise char s[15] = "Abracadabra"; char *found = strstr(s,"dab"); cout << found; // prints dabra 12-17

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Conversions Between Numbers and Strings "1416" is a string; 1416 without quotes is an int There are classes that can be used to convert between string and numeric forms of numbers Need to include sstream header file 12-18

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conversion Classes istringstream: –contains a string to be converted to numeric values where necessary –Use str(s) to initialize string to contents of s –Use the stream extraction operator >> to read from the string ostringstream: –collects a string in which numeric data is converted as necessary –Use the stream insertion operator << to add data onto the string –Use str() to retrieve converted string 12-19

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley atoi and atol atoi converts alphanumeric to int atol converts alphanumeric to long int atoi(char *numericStr) long atol(char *numericStr) Examples: int number; long lnumber; number = atoi("57"); lnumber = atol("50000"); 12-20

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley atof atof converts a numeric string to a floating point number, actually a double double atof(char *numericStr) Example: double dnumber; dnumber = atof(" "); 12-21

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley atoi, atol, atof if C-string being converted contains non- digits, results are undefined –function may return result of conversion up to first non-digit –function may return 0 All functions require cstdlib 12-22

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley itoa itoa converts an int to an alphanumeric string Allows user to specify the base of conversion itoa(int num, char *numStr, int base) Example: To convert the number 1200 to a hexadecimal string char numStr[10]; itoa(1200, numStr, 16); The function performs no bounds-checking on the array numStr 12-23

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Character Testing require cctype header file FUNCTIONMEANING isalpha true if arg. is a letter, false otherwise isalnum true if arg. is a letter or digit, false otherwise isdigit true if arg. is a digit 0-9, false otherwise islowertrue if arg. is lowercase letter, false otherwise 12-24

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Writing Your Own C-String Handling Functions When writing C-String Handling Functions: –can pass arrays or pointers to char –can perform bounds checking to ensure enough space for results –can anticipate unexpected user input 12-25

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.5 More About the C++ string Class The string class offers several advantages over C-style strings: –large body of member functions –overloaded operators to simplify expressions Need to include the string header file 12-26

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley string class constructors Default constructor string() Copy constructor string(string&) initializes string objects with values of other string objects Convert constructor string(char *) initializes string objects with values of C- strings Various other constructors 12-27

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Overloaded string Operators OPERATORMEANING >> reads whitespace-delimited strings into string object << inserts string object into a stream = assigns string on right to string object on left += appends string on the right to the end of contents of string on left 12-28

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Overloaded string Operators (continued) OPERATORMEANING + Returns concatenation of the two strings [] references character in string using array notation >, >=, <, <=, ==, != relational operators for string comparison. Return true or false 12-29

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Overloaded string Operators string word1, phrase; string word2 = " Dog"; cin >> word1; // user enters "Hot" // word1 has "Hot" phrase = word1 + word2; // phrase has // "Hot Dog" phrase += " on a bun"; for (int i = 0; i < 16; i++) cout << phrase[i]; // displays // "Hot Dog on a bun" 12-30

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley string Member Functions Categories: –conversion to C-strings: c_str, data –modification: append, assign, clear, copy, erase, insert, replace, swap –space management: capacity, empty, length, resize, size –substrings: find, substr –comparison: compare 12-31

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conversion to C-strings data() and c_str() both return the C- string equivalent of a string object Useful when using a string object with a function that is expecting a C-string char greeting[20] = "Have a "; string str("nice day"); strcat(greeting, str.data()); 12-32

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Modification of string objects str.append(string s) appends contents of s to end of str Convert constructor for string allows a C- string to be passed in place of s string str("Have a "); str.append("nice day"); append is overloaded for flexibility 12-33

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Modification of string objects str.insert(int pos, string s) inserts s at position pos in str Convert constructor for string allows a C- string to be passed in place of s string str("Have a day"); str.insert(7, "nice "); insert is overloaded for flexibility 12-34

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.6 Creating Your Own String Class A good way to put OOP skills into practice The class allocates dynamic memory, so has copy constructor, destructor, and overloaded assignment Overloads the stream insertion and extraction operators, and many other operators 12-35

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda