Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.

Similar presentations


Presentation on theme: "Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University."— Presentation transcript:

1 Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University

2 Slide 2 Objectives Understand the difference between c-string and string Understand how to use string operations

3 Slide 3 String There are 2 kinds of strings: 1. C-string: array that comes from C language 1. Array of characters that end with ‘\0’ 2. Any array initialized using double quotes 3. Must use #include 2. String: a class 1. Object with character array member value 2. #include

4 Slide 4 String (Cont.) The member data (array of characters) automatically expands its length as needed Has constructor: 1. Default constructor: initialize member as empty string Example 1: string empty_string; 2. Constructor: convert c-string (double quotes) to member data Example 2: string one_string (“c-string value”);

5 Slide 5 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 ‘-’

6 Slide 6 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

7 Slide 7 String Member Functions 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

8 Slide 8 String Member Functions (Cont.) Assignment: stringcopy = oldstring Example: string second_string; second_string = one_string.substr (2,3); Concatenation: concatstring = string1 + string2 Example: string third_string; third_string = one_string + second_string;

9 Slide 9 String Member Functions (Cont.) modification: 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-”

10 Slide 10 String Member Functions (Cont.) Compare: string1 == string2 string1 != string2 string1 string2 string1 = string2 Example: if (second_string == one_string) …

11 Slide 11 String Member Functions (Cont.) 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

12 Slide 12 String Member Functions (Cont.) Convert to c-string: returns double quotes c-string c-str() Example: one_string.c-str() returns “c-string value”

13 Slide 13 Keyboard Input Function - cin Form: cin >> string_variable; Read from keyboard into string_variable until a white space 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’

14 Slide 14 Keyboard Input Function - getline 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”

15 Slide 15 Array of Pointers Store array of strings: save space 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”

16 Slide 16 Array of Pointers – Method 1 (Cont.) Example: (Cont.) After bubble sort, the array w contains 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”

17 Slide 17 Array of Strings Example: Sort strings Program Data

18 Slide 18 References Deitel & Deitel: C++ How to Program, 4th ed., Chapter 15, Prentice Hall


Download ppt "Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University."

Similar presentations


Ads by Google