Null-Terminated Strings. McGraw-Hill The Complete Reference Fourth Edition
ಜಾಹಿರಾತು
ಜಾಹಿರಾತು
94
C + + : T h e C o m p l e t e R e f e r e n c e
Null-Terminated Strings
By far the most common use of the one-dimensional array is as a character string.
C++ supports two types of strings. The first is the null-terminated string, which is a null-terminated character array. (A null is zero.) Thus a null-terminated string contains the characters that comprise the string followed by a null. This is the only type of string defined by C, and it is still the most widely used. Sometimes null-terminated strings are called C-strings. C++ also defines a string class, called string, which provides an object-oriented approach to string handling. It is described later in this book. Here, null-terminated strings are examined.
When declaring a character array that will hold a null-terminated string, you need to declare it to be one character longer than the largest string that it is to hold. For example, to declare an array str that can hold a 10-character string, you would write char str[11];
This makes room for the null at the end of the string.
When you use a quoted string constant in your program, you are also creating a null-terminated string. A string constant is a list of characters enclosed in double quotes.
For example,
"hello there"
You do not need to add the null to the end of string constants manually—the compiler does this for you automatically.
C/C++ supports a wide range of functions that manipulate null-terminated strings.
The most common are
Name strcpy(s1, s2) strcat(s1, s2) strlen(s1) strcmp(s1, s2) strchr(s1, ch) strstr(s1, s2)
Function
Copies s2 into s1.
Concatenates s2 onto the end of s1.
Returns the length of s1.
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
Returns a pointer to the first occurrence of ch in s1.
Returns a pointer to the first occurrence of s2 in s1.
These functions use the standard header file string.h. (C++ programs can also use the
C++-style header <cstring>.) The following program illustrates the use of these string functions:
C h a p t e r 4 : A r r a y s a n d N u l l - T e r m i n a t e d S t r i n g s
95
#include <stdio.h>
#include <string.h> int main(void)
{ char s1[80], s2[80]; gets(s1); gets(s2); printf("lengths: %d %d\n", strlen(s1), strlen(s2)); if(!strcmp(s1, s2)) printf("The strings are equal\n"); strcat(s1, s2); printf("%s\n", s1); strcpy(s1, "This is a test.\n"); printf(s1); if(strchr("hello", 'e')) printf("e is in hello\n"); if(strstr("hi there", "hi")) printf("found hi");
} return 0;
If you run this program and enter the strings "hello" and "hello", the output is lengths: 5 5
The strings are equal hellohello
This is a test.
e is in hello found hi
Remember, strcmp( ) returns false if the strings are equal. Be sure to use the logical operator ! to reverse the condition, as just shown, if you are testing for equality.
Although C++ defines a string class, null-terminated strings are still widely used in existing programs. They will probably stay in wide use because they offer a high level of efficiency and afford the programmer detailed control of string operations.
However, for many simple string-handling chores, C++'s string class provides a convenient alternative.
ಡೌನ್ಲೋಡ್ ಮಾಡಿ
ಜಾಹಿರಾತು