|

A
string is a series of characters. The only strings
you’ve seen until now have been unnamed string constants
used in cout statements, such as
cout
<< "hello world.\n";
In
C++ a string is an array of chars ending with a
null character. You can declare and initialize a
string just as you would any other array. For example,
char
Greeting[] =
{
‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’,’o’,’r’,’l’,’d’,
‘\0’ };
The
last character, ‘\0’, is the null character, which
many C++ functions recognize as the terminator for
a string. Although this character-by-character approach
works, it is difficult to type and admits too many
opportunities for error. C++ enables you to use
a shorthand form of the previous line of code. It
is
char
Greeting[] = "Hello World";
You
should note two things about this syntax:
·
Instead of single quoted characters separated by
commas and surrounded by braces, you have a double-quoted
string, no commas, and no braces.
·
You don’t need to add the null character because
the compiler adds it for you.
The
string Hello World is 12 bytes. Hello is 5 bytes,
the space 1, World 5, and the null character 1.
You
can also create uninitialized character arrays.
As with all arrays, it is important to ensure that
you don’t put more into the buffer than there is
room for.
Listing
11.8 demonstrates the use of an uninitialized buffer.
Listing
11.8. Filling an array.
1:
//Listing 11.8 char array buffers
2:
3:
#include <iostream.h>
4:
5:
int main()
6:
{
7:
char buffer[80];
8:
cout << "Enter the string: ";
9:
cin >> buffer;
10:
cout << "Here’s the buffer: " << buffer
<< endl;
11:
return 0;
12:
}
OUTPUT:
Enter the string: Hello World
Here’s the buffer: Hello
ANALYSIS: On line 7, a buffer is declared
to hold 80 characters. This is large enough to hold
a 79-character string and a terminating null character.
On line 8, the user is prompted to enter a string,
which is entered into buffer on line 9. It is the
syntax of cin to write a terminating null to buffer
after it writes the string.
There
are two problems with the program in Listing 11.8.
First, if the user enters more than 79 characters,
cin writes past the end of the buffer. Second, if
the user enters a space, cin thinks that it is the
end of the string, and it stops writing to the buffer.
To
solve theseproblems, you must call a special method
on cin: get(). cin.get() takes three parameters:
The
buffer to fill
The
maximum number of characters to get
The
delimiter that terminates input
The
default delimiter is newline. Listing 11.9 illustrates
its use.
Listing
11.9. Filling an array.
1:
//Listing 11.9 using cin.get()
2:
3:
#include <iostream.h>
4:
5:
int main()
6:
{
7:
char buffer[80];
8:
cout << "Enter the string: ";
9:
cin.get(buffer, 79); // get up to 79 or newline
10:
cout << "Here’s the buffer: " << buffer
<< endl;
11:
return 0;
12:
}
OUTPUT:
Enter the string: Hello World
Here’s the buffer: Hello World
ANALYSIS: Line 9 calls the method get()
of cin. The buffer declared in line 7 is passed
in as the first argument. The second argument is
the maximum number of characters to get. In this
case, it must be 79 to allow for the terminating
null. There is no need to provide a terminating
character because the default value of newline is
sufficient.
strcpy()
and strncpy()
C++
inherits from C a library of functions for dealing
with strings. Among the many functions provided
are two for copying one string into another: strcpy()
and strncpy(). strcpy() copies the entire contents
of one string into a designated buffer. Listing
11.10 demonstrates the use of strcpy().
Listing
11.10. Using strcpy().
1:
#include <iostream.h>
2:
#include <string.h>
3:
int main()
4:
{
5:
char String1[] = "No man is an island";
6:
char String2[80];
7:
8:
strcpy(String2,String1);
9:
10:
cout << "String1: " << String1 <<
endl;
11:
cout << "String2: " << String2 <<
endl;
12:
return 0;
13:
}
OUTPUT:
String1: No man is an island
String2: No man is an island
ANALYSIS: The header file string.h
is included in line 2. This file contains the prototype
of the strcpy() function. strcpy() takes two character
arrays—a destination followed by a source. If the
source were larger than the destination, strcpy()
would overwrite past the end of the buffer.
To protect against this, the standard library also
includes strncpy(). This variation takes a maximum
number of characters to copy. strncpy() copies up
to the first null character or the maximum number
of characters specified into the destination buffer.
Listing
11.11 illustrates the use of strncpy().
Listing
11.11. Using strncpy().
1:
#include <iostream.h>
2:
#include <string.h>
3:
int main()
4:
{
5:
const int MaxLength = 80;
6:
char String1[] = "No man is an island";
7:
char String2[MaxLength+1];
8:
9:
10:
strncpy(String2,String1,MaxLength);
11:
12:
cout << "String1: " << String1 <<
endl;
13:
cout << "String2: " << String2 <<
endl;
14:
return 0;
15:
}
OUTPUT:
String1: No man is an island
String2: No man is an island
ANALYSIS: In line 10, the call to strcpy()
has been changed to a call to strncpy(), which takes
a third parameter: the maximum number of characters
to copy. The buffer String2 is declared to take
MaxLength+1 characters. The extra character is for
the null, which both strcpy() and strncpy() automatically
add to the end of the string.
|