Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages 490-508 in Chapter 7, and pages 550-558 in Chapter 8.  Homework #9 and Lab #9 due next week.

Similar presentations


Presentation on theme: "EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages 490-508 in Chapter 7, and pages 550-558 in Chapter 8.  Homework #9 and Lab #9 due next week."— Presentation transcript:

1 EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages 490-508 in Chapter 7, and pages 550-558 in Chapter 8.  Homework #9 and Lab #9 due next week.  Quiz next week.

2 Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

3 string Type To use data type string, a program must include the header file string. A string is a sequence of 0 or more characters. – The first character is in position 0. – The second character is in position 1, etc. Strings in C++ are enclosed in "". Binary operator + performs the string concatenation operation. Array subscript operator [] allows access to an individual character in a string. 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition

4 Additional string Operations 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition

5 Example 7-18: swap Function 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition

6 Chapter 8 Arrays and Strings

7 C -Strings (Character Arrays) Character array: an array whose components are of type char. C -strings are null-terminated ( '\0' ) character arrays. Example: – 'A' is the character A. – "A" is the C -string A. – "A" represents two characters, 'A' and '\0'. 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition

8 C -Strings (Character Arrays) (cont’d.) Example: char name[16]; Since C -strings are null terminated and name has 16 components, the largest string it can store has 15 characters. If you store a string whose length is less than the array size, the last components are unused. 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition

9 C -Strings (Character Arrays) (cont’d.) Size of an array can be omitted if the array is initialized during declaration. Example: char name[] = "John"; – Declares an array of length 5 and stores the C- string "John" in it. Useful C-string manipulation functions – strcpy, strcmp, and strlen 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition

10 C-String Comparison C -strings are compared character by character using the collating sequence of the system. – Use the function strcmp If using the ASCII character set: – "Air" < "Boat" – "Air" < "An" – "Bill" < "Billy" – "Hello" < "hello" 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition

11 Reading and Writing C-Strings Most rules for arrays also apply to C -strings (which are character arrays). C++ does not allow aggregate operations, including assignment, comparison, input, or output on arrays in general. But C++ does allow aggregate input and output of C -strings. 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition

12 C-String Input Example: cin >> name; – Stores the next inputted C-string into name. To read C-strings with blanks, use get function: cin.get(str, m+1); – Stores the next m characters into str but the newline character is not stored in str. – If input string has fewer than m characters, reading stops at the newline character. 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition

13 C-String Output Example: 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 strange output may occur. << continues to output data from memory adjacent to name until a '\0' is found. 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition

14 Specifying Input/Output Files at Execution Time User can specify the name of an input and/or output file at execution time: 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition

15 string Type and Input/Output Files Argument to the open function must be a null- terminated string (a C -string). – If using a string variable for the name of an I/O file, the value must first be converted to a C -string before calling open. – Use the c_str function to convert. Syntax: strVar.c_str() where strVar is a variable of type string. 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition


Download ppt "EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages 490-508 in Chapter 7, and pages 550-558 in Chapter 8.  Homework #9 and Lab #9 due next week."

Similar presentations


Ads by Google