240 likes | 413 Views
C++ Programming: Program Design Including Data Structures, Fifth Edition. Chapter 10: Strings and string type. Objectives. 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.
E N D
C++ Programming:Program Design IncludingData Structures, Fifth Edition Chapter 10: Strings and string type
Objectives • 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
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
Example 8-14: clear, empty, erase, length, AND size FUNCTIONS 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: findFUNCTION • - 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. // 4294967295
Example 8-16: insert AND replace FUNCTIONS • - 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 - strVar.substr(pos, len): returning a string that is a substring of strVar starting at pos
Example 8-18: swap FUNCTION - strVar.swap(str): swaps the contents of strVar and str.
C-Strings (Character Arrays) • 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.) • 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.) • 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";
String Comparison • 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.) Example_9-6.cpp
Reading and Writing Strings • 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 • 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 • 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 • 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 cin.get.cpp ‘ Answer: str1={‘C’,‘+’,‘\0’} str2={‘+’,‘ ’,‘P’,‘\0’} Final output: C++ P
Class Activity • 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 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 • Strings in C++ can be manipulated using either the data type string or character arrays (C-strings)
Arrays of Strings and the string Type • 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
Summary • In C++, C-strings are null terminated and are stored in character arrays • Commonly used C-string manipulation functions include: • strcpy, strcmp, and strlen