Chapter 16 C Strings n n n By
Chapter 16 C++ Strings n n n By C. Shing ITEC Dept Radford University
Objectives n n Understand the difference between c-string and string Understand how to use string operations 2
String There are 2 kinds of strings: 1. C-string: array that comes from C language 1. 2. 3. Array of characters that end with ‘ ’ Any array initialized using double quotes Must use #include <cstring> String: a class 2. 1. 2. Object with character array member value #include <string> 3
String (Cont. ) The member data (array of characters) automatically expands its length as needed Has constructor: n n 1. 2. Default constructor: initialize member as empty string Example 1: string empty_string; Constructor: convert c-string (double quotes) to member data Example 2: string one_string (“c-string value”); 4
Array (Cont. ) Can access individual character of the member data using index or member function at Example 3: (from Example 2) one_string [0] has the character ’c’ Or one_string. at (1) has the character ‘-’ n 5
Array (Cont. ) Note: access individual character of the member data using index is not recommended, Even though using overlimit index will not get error: Example: one_string [100] will include undesired character 6
String Member Functions n n Substring: substr (start_position, length) Example: one_string. substr (2, 3) contains “str” Check content: empty() Example: empty_string. empty() is true one_string. empty() is false 7
String Member Functions (Cont. ) n n Assignment: stringcopy = oldstring Example: string second_string; second_string = one_string. substr (2, 3); Concatenation: concatstring = string 1 + string 2 Example: string third_string; third_string = one_string + second_string; 8
String Member Functions (Cont. ) modification: n 1. insert (start_position, insertedstring) Example: string second_string; second_string = one_string (2, “new”); contains “c-new” 2. remove (position, length) Example: string second_string; second_string = one_string. remove (2, 6); contains “c-” 9
String Member Functions (Cont. ) Compare: string 1 == string 2 string 1 != string 2 string 1 < string 2 string 1 > string 2 string 1 <= string 2 string 1 >= string 2 Example: if (second_string == one_string) … n 10
String Member Functions (Cont. ) n Search: returns index find (searched_string) find (searched_string, searched_position) find_first_of (searched_string, searched_position) find_first_not_of (searched_string, searched_position) Example: one_string. find (“string”) returns 2 11
String Member Functions (Cont. ) n Convert to c-string: returns double quotes c-string c-str() Example: one_string. c-str() returns “c-string value” 12
Keyboard Input Function - cin Form: cin >> string_variable; Read from keyboard into string_variable until a white space n Example: string s; // remember s is address, do not use char *s cin >> s; // sample data: a 100 -1. 23 This is a sample data // s contains member data: array contains character ‘a’ 13
Keyboard Input Function - getline n Form: getline (cin, string_variable); Or getline (cin, string_variable, stop_character); Read from keyboard into string_variable until the stop_character Example: string s; // remember s is address, do not use char *s getline (cin, s, ‘n’); // sample data: a 100 -1. 23 This is a sample data // s contains member data: “a 100 -1. 23 This is a sample data” 14
Array of Pointers Store array of strings: save space n Example: Assume that array w has 5 cells, each stores an address of strings as follows: w[0]=100=address of string “this” w[1]=200=address of string “is” w[2]=300=address of string “a” w[3]=400=address of string “snow” w[4]=500=address of string “day” n 15
Array of Pointers – Method 1 (Cont. ) Example: (Cont. ) After bubble sort, the array w contains n w[0]=300=address of string “a” w[1]=500=address of string “day” w[2]=200=address of string “is” w[3]=400=address of string “snow” w[4]=100=address of string “this” 16
Array of Strings n Example: Sort strings Program Data 17
References Deitel & Deitel: C++ How to Program, 4 th ed. , Chapter 15, Prentice Hall n 18
- Slides: 18