22
Chapter 9 Strings

Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

Chapter 9

Strings

Page 2: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

Learning ObjectivesAn Array Type for Strings

C-StringsC-String Manipulation FunctionsC-String input and output

Page 3: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

IntroductionC-strings

Array with base type charEnd of string marked with null, ‘\0’‘Older’ method inherited from C

Page 4: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-StringsArray with base type char

One character per indexed variableOne extra character: ‘\0’

Called ‘null character’End marker

We’ve used c-stringsLiteral “Hello” stored as c-string

Page 5: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String VariableArray of characters:char s[10];

Declares a c-string variable to hold up to 9characters+ one null character

Typically ‘partially-filled’ arrayDeclare large enough to hold max-size stringIndicate end with null

Only difference from standard array:Must contain null character

Page 6: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String StorageA standard array:char s[10];

If s contains string “Hi Mom”, stored as:

Display page 352

Page 7: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String InitializationCan initialize c-string:char myMessage[20] = “Hi there.”;

Needn’t fill entire arrayInitialization places ‘\0’ at end

Can omit array-size:char shortString[] = “abc”;

Automatically makes size one more thanlength of quoted stringNOT same as:char shortString[] = {‘a’, ‘b’, ‘c’};

Page 8: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String IndexesA c-string IS an arrayCan access indexed variables of:char ourString[5] = “Hi”;

ourString[0] is ‘H’ourString[1] is ‘i’ourString[2] is ‘\0’ourString[3] is unknownourString[4] is unknown

Page 9: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String Index ManipulationCan 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!

Page 10: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

LibraryDeclaring c-strings

Requires no C++ libraryBuilt into standard C++

ManipulationsRequire library <cstring>Typically included when using c-strings

Normally want to do ‘fun’ things with them

Page 11: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

= and == with C-stringsC-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!

Page 12: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

Comparing C-stringsAlso 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.”;else

cout << “Strings are same.”;

Page 13: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

The <cstring> LibraryFull of string manipulation functions

Display 9.1, page 357

Page 14: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

The <cstring> Library Cont’dFull of string manipulation functions

Display 9.1, page 357

Page 15: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-string Functions: strlen()‘String length’Often useful to know string length:char myString[10] = “dobedo”;cout << strlen(myString);

Returns number of charactersNot including null

Result here:6

Page 16: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

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!

Page 17: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-string Arguments and ParametersRecall: c-string is arraySo c-string parameter is array parameter

C-strings passed to functions can be changedby receiving function!

Like all arrays, typical to send size as wellFunction ‘could’ also use ‘\0’ to find endSo size not necessary if function won’t changec-string parameterUse ‘const’ modifier to protect c-stringarguments

Page 18: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String OutputCan output with insertion operator, <<As we’ve been doing already:cout << news << “ Wow.\n”;

Where news is a c-string variablePossible because << operator isoverloaded for c-strings!

Page 19: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String InputCan input with extraction operator, >>

Issues exist, howeverWhitespace is ‘delimiter’

Tab, space, line breaks are ‘skipped’Input reading ‘stops’ at delimiter

Watch size of c-stringMust be large enough to hold entered string!C++ gives no warnings of such issues!

Page 20: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String Input Examplechar a[80], b[80];cout << “Enter input: “;cin >> a >> b;cout << a << b << “END OF OUTPUT\n”;Dialogue offered:

Enter input: Do be do to you!DobeEND OF OUTPUTNote: Underlined portion typed at keyboard

C-string a receives: “do”C-string b receives: “be”

Page 21: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

C-String Line InputCan receive entire line into c-stringUse getline(), a predefined memberfunction:char a[80];cout << “Enter input: “;cin.getline(a, 80);cout << a << “END OF OUTPUT\n”;

Dialogue:Enter input: Do be do to you!Do be do to you!END OF INPUT

Page 22: Chapter 9alfuqaha/cs111/lectures/lec10.pdf · 2013. 8. 3. · Chapter 9 Strings. Learning Objectives An Array Type for Strings C-Strings C-String Manipulation Functions C-String input

More getline()Can explicitly tell length to receive:char shortString[5];cout << “Enter input: “;cin.getline(shortString, 5);cout << shortString << “END OF OUTPUT\n”;

Results:Enter input: dobedowapdobeEND OF OUTPUTForces FOUR characters only be read

Recall need for null character!