Chapter 9 ABSOLUTE C++ Strings WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 9 Strings
Learning Objectives An Array Type for Strings ◆C-Strings Character Manipulation Tools ◆Character I/o get,put member functions putback,peek,ignore Standard Class string ◆String processing Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives ¨ An Array Type for Strings ¨C-Strings ¨ Character Manipulation Tools ¨Character I/O ¨get, put member functions ¨putback, peek, ignore ¨ Standard Class string ¨String processing
Introduction ◆Two string types: C-strings Array with base type char End of string marked with null,"\O" "Older"method inherited from C ◆String class ◆Uses templates Copyright006 Pearson Addison-Wesley.All rights reserved. 9-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-3 Introduction ¨ Two string types: ¨ C-strings ¨Array with base type char ¨End of string marked with null, "\0" ¨"Older" method inherited from C ¨ String class ¨Uses templates
C-Strings Array with base type char One character per indexed variable One extra character:"10" Called "null character" ◆End marker ◆We've used c-strings Literal "Hello"stored as c-string Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-4 C-Strings ¨ Array with base type char ¨One character per indexed variable ¨One extra character: "\0" ¨Called "null character" ¨End marker ¨ We’ve used c-strings ¨Literal "Hello" stored as c-string
C-String Variable ◆Array of characters: char s[10]; Declares a c-string variable to hold up to 9 characters ◆+one null character Typically "partially-filled"array Declare large enough to hold max-size string Indicate end with null Only difference from standard array: Must contain null character Copyright006 Pearson Addison-Wesley.All rights reserved. 9-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-5 C-String Variable ¨ Array of characters: char s[10]; ¨ Declares a c-string variable to hold up to 9 characters ¨ + one null character ¨ Typically "partially-filled" array ¨ Declare large enough to hold max-size string ¨ Indicate end with null ¨ Only difference from standard array: ¨ Must contain null character