Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 41 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
41
Dung lượng
245,97 KB
Nội dung
C and C++ Strings
An Array Type for Strings
• C-strings can be used to represent strings of
characters
– C-strings are stored as arrays of characters
– C-strings use the null character '\0' to end a string
• The Null character is a single character
– To declare a C-string variable, declare an array of
characters:
char s[11];
C-string Details
• Declaring a C-string as char s[10] creates space
for only nine characters
– The null character terminator requires one space
• A C-string variable does not need a size
variable
– The null character immediately follows the last
character of the string
• Example:
s[0]
s[1]
H i
s[2]
s[3]
M
s[4]
o
s[5]
s[6]
s[7]
m
!
\0
s[8]
s[9]
?
?
Initializing a C-string
• To initialize a C-string during declaration:
char my_message[20] = "Hi there.";
– The null character '\0' is added for you
Is called a
string constant
or string literal
• Another alternative:
char short_string[ ] = "abc";
but not this:
char short_string[ ] = {'a', 'b', 'c'};
– because null character '\0' will NOT be added
Length of C Strings
To calculate the length of a string use the strlen() function – returns the
number of characters between the start of the string to the terminating
character:
char name[20] = "Jim";
strlen(name) 3
This should not be confused with the size of the array that holds the string.
sizeof(name) 20;
char emptystr[10] = ""; // same as:
char emptystr[10] = {'\0'}; // or = {0};
strlen(emptystr) 0
sizeof(emptystr) 10;
What about
char empty[50];
strlen (empty) ???
Assignment With C-strings
• This statement is illegal:
char my_name[20], your_name[20];
my_name = "Thao"; ERROR!
– This is an assignment statement, not an
initialization
– The assignment operator does not work with
C-strings (just like it doesn’t work for arrays)
my_name = your_name; ERROR!
•
Assignment of C-strings
• To assign a value to a C-string variable is to use
strcpy, defined in the cstring library
#include
char my_name[20];
strcpy (my_name, "Thao");
Places " Thao" followed by the null character
in string my_name.
A Problem With strcpy
• strcpy can create problems if not used
carefully
– strcpy does not check the declared length of the
first argument
– It is possible for strcpy to write characters beyond
the declared size of the array:
char my_name[10];
strcpy (my_name, "Thao Thi Nguyen");
this will overwrite memory!
A Solution for strcpy
• A safer version of strcpy is named strncpy
– strncpy uses a third argument representing the
maximum number of characters to copy
– Example:
char another_string[10];
strncpy(another_string,
a_string_variable, 9);
another_string[9] = ‘\0’;
This code copies up to 9 characters into
another_string, leaving one space for '\0'
Comparing C-strings
• The = = operator does not work with C-strings
– The predefined function strcmp is used to compare C-string
variables
– Example: #include
…
char name1[20], name2[20];
if (strcmp(name1, name2))
cout [...]...strcmp's logic strcmp(s1, s2) == 0 if strings are the same strcmp(s1, s2) != 0 if strings are different strcmp(s1, s2) < 0 if s1 precedes (comes before) s2 strcmp(s1, s2) > 0 if s2 precedes s1 strcmp examples cout 0 cout 1 cout -1 cout -1 ... for Strings • C- strings can be used to represent strings of characters – C- strings are stored as arrays of characters – C- strings use the null character '' to end a string • The Null character... a single character – To declare a C- string variable, declare an array of characters: char s[11]; C- string Details • Declaring a C- string as char s[10] creates space for only nine characters –... ); replace( ); Converting C+ + string to C- string Converting C- string to C+ + string • Happens automatically in most cases string s = "abc"; • Can force using string("abc") string s = "abc" + "def";