egr 2261 unit 9 strings and c-strings read malik, pages 490-508 in chapter 7, and pages 550-558 in...
TRANSCRIPT
EGR 2261 Unit 9Strings 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.
Chapter 7:User-Defined Simple Data Types,
Namespaces, and the string Type
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
Additional string Operations
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Example 7-18: swap Function
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Chapter 8Arrays and Strings
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
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
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
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
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
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
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
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
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