C-strings

advertisement
C++ PROGRAMMING:
PROGRAM DESIGN INCLUDING
DATA STRUCTURES, FIFTH EDITION
Chapter 10: Strings and string type
Objectives
2
•
•
•
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
3


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.)
4

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
5
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: find FUNCTION
6
- 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
7
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
8
- strVar.substr(pos, len): returning a string
that is a substring of strVar starting at pos
Example 8-18: swap FUNCTION
9
- strVar.swap(str): swaps the contents of strVar and str.
C-Strings (Character Arrays)
10

Character array: an array whose components are of type
char

C-strings is an array of characters ending with the nullterminated ('\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.)
11

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
first 11 components of name are used and the
last five are left unused
 The
C-Strings (Character Arrays) (cont'd.)
12
•
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 Cstring "John" in it.
char name[16] = {‘J’,’o’,’h’,’n’,’\0’};
=
char name[] = "John";
C-Strings (Character Arrays) (cont'd.)
13
String Comparison
14


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.)
15
Example_9-6.cpp
Reading and Writing Strings
16
•
•
•
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 Cstrings (that is, character arrays)
String Input
17



Assume we declare char name[31];
cin >> name; stores the next input Cstring into name
To read strings with blanks, use get:
cin.get(str, m+1);
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
 Stores
String Output
18

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
19

Consider the following statement:
cin.get.cpp
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
Answer:
str1={‘C’,‘+’,‘\0’}
str2={‘+’,‘ ’,‘P’,‘\0’}
Final output:
C++ P

Class Activity
20


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:
Answer:
C++ Programming
str1={‘C’,‘+’,,‘+’,‘ ’,‘P’,‘r’,‘o’,
‘g’,‘r’,‘a’,‘m’,‘m’,‘i’,‘n’,‘g’,‘\0’}
C++ Programming
str2={‘C’,‘+’,‘+’,‘\0’}
Final output:
C++ ProgrammingC++
Arrays of Strings
21

Strings in C++ can be manipulated using either the
data type string or character arrays (C-strings)
Arrays of Strings and the string Type
22

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
23
Arrays of Strings and C-Strings
(Character Arrays)
Summary
24


In C++, C-strings are null terminated and are stored
in character arrays
Commonly used C-string manipulation functions
include:
 strcpy,
strcmp, and strlen
Download