DOC PREVIEW
FSU COP 3330 - COP 3330 Lecture Notes

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 9Learning ObjectivesIntroductionC-StringsC-String VariableC-String StorageC-String InitializationC-String IndexesC-String Index ManipulationLibrary= and == with C-stringsComparing C-stringsThe <cstring> Library: Display 9.1 Some Predefined C-String Functions in <cstring> (1 of 2)The <cstring> Library: Display 9.1 Some Predefined C-String Functions in <cstring> (2 of 2)C-string Functions: strlen()C-string Functions: strcat()C-string Arguments and ParametersC-String OutputC-String InputC-String Input ExampleC-String Line InputMore getline()Character I/OMember Function get()Member Function put()More Member FunctionsCharacter-Manipulating Functions: Display 9.3 Some Functions in <cctype> (1 of 3)Character-Manipulating Functions: Display 9.3 Some Functions in <cctype> (2 of 3)Character-Manipulating Functions: Display 9.3 Some Functions in <cctype> (3 of 3)Standard Class stringDisplay 9.4 Program Using the Class stringI/O with Class stringgetline() with Class stringOther getline() VersionsPitfall: Mixing Input MethodsClass string ProcessingDisplay 9.7 Member Functions of the Standard Class string (1 of 2)Display 9.7 Member Functions of the Standard Class string (2 of 2)C-string and string Object ConversionsSummaryChapter 9StringsCopyright © 2008 Pearson Addison-Wesley. All rights reservedLearning 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 processing9-2Copyright © 2008 Pearson Addison-Wesley. All rights reserved.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 templates9-3Copyright © 2008 Pearson Addison-Wesley. All rights reserved.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-string9-4Copyright © 2008 Pearson Addison-Wesley. All rights reserved.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 character9-5Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-String Storage•A standard array:char s[10];–If s contains string "Hi Mom", stored as:–Display, page 3709-6Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-String Initialization•Can initialize c-string:char myMessage[20] = "Hi there.";–Needn’t fill entire array–Initialization places "\0" at end•Can omit array-size:char shortString[] = "abc";–Automatically makes size one more thanlength of quoted string–NOT same as:char shortString[] = {"a", "b", "c"};9-7Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-String Indexes•A c-string IS an array•Can access indexed variables of:char ourString[5] = "Hi";–ourString[0] is "H"–ourString[1] is "i"–ourString[2] is "\0"–ourString[3] is unknown–ourString[4] is unknown9-8Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-String Index Manipulation•Can manipulate indexed variableschar happyString[7] = "DoBeDo";happyString[6] = "Z";–Be careful!–Here, "\0" (null) was overwritten by a "Z"!•If null overwritten, c-string no longer "acts" like c-string!–Unpredictable results!9-9Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Library•Declaring c-strings–Requires no C++ library–Built into standard C++•Manipulations–Require library <cstring>–Typically included when using c-strings•Normally want to do "fun" things with them9-10Copyright © 2008 Pearson Addison-Wesley. All rights reserved.= and == with C-strings•C-strings not like other variables–Cannot assign or compare:char aString[10];aString = "Hello"; // ILLEGAL!•Can ONLY use "=" at declaration of c-string!•Must use library function for assignment:strcpy(aString, "Hello");–Built-in function (in <cstring>)–Sets value of aString equal to "Hello"–NO checks for size!•Up to programmer, just like other arrays!9-11Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Comparing C-strings•Also cannot use operator ==char aString[10] = "Hello";char anotherString[10] = "Goodbye";–aString == anotherString; // NOT allowed!•Must use library function again:if (strcmp(aString, anotherString))cout << "Strings NOT same.";elsecout << "Strings are same.";9-12Copyright © 2008 Pearson Addison-Wesley. All rights reserved.The <cstring> Library: Display 9.1 Some Predefined C-String Functions in <cstring> (1 of 2)•Full of string manipulation functions9-13Copyright © 2008 Pearson Addison-Wesley. All rights reserved.The <cstring> Library: Display 9.1 Some Predefined C-String Functions in <cstring> (2 of 2)9-14Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-string Functions: strlen()•"String length"•Often useful to know string length:char myString[10] = "dobedo";cout << strlen(myString);–Returns number of characters•Not including null–Result here:69-15Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-string Functions: strcat()•strcat()•"String concatenate":char stringVar[20] = "The rain";strcat(stringVar, "in Spain");–Note result:stringVar now contains "The rainin Spain"–Be careful!–Incorporate spaces as needed!9-16Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-string Arguments and Parameters•Recall: c-string is array•So c-string parameter is array parameter–C-strings passed to functions can be changedby receiving function!•Like all arrays, typical to send size as well–Function "could" also use "\0" to find end–So size not necessary if function won’t changec-string parameter–Use "const" modifier to protect c-string arguments9-17Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-String Output•Can output with insertion operator, <<•As we’ve been doing already:cout << news << " Wow.\n";–Where news is a c-string variable•Possible because << operator isoverloaded for c-strings!9-18Copyright © 2008 Pearson Addison-Wesley. All rights reserved.C-String Input•Can input with extraction operator, >>–Issues exist, however•Whitespace is "delimiter"–Tab, space, line


View Full Document

FSU COP 3330 - COP 3330 Lecture Notes

Download COP 3330 Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view COP 3330 Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view COP 3330 Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?