Strings


1.1.   Strings

By far, the most common use for one-dimensional arrays is to create character strings. In C++, a string is defined as a character array that is terminated by a null. A null character is specified using '\0', and is zero. Because of the null terminator, it is necessary to declare a character array to be one character longer than the largest string that it will hold. For example, if you want to declare an array str that could hold a 10-character string, you would write:
char str[11];
Specifying the size as 11 makes room for the null at the end of the string. As you learned earlier in this book, C++ allows you to define a string literal. Recall that a string literal is a list of characters enclosed in double quotes. Here are some examples:

"hello there" "I like C++"
"#$%@@#$" ""
The last string shown is "". This is called a null string. It contains only the null terminator, and no other characters. Null strings are useful because they represent the empty string. It is not necessary to manually add the null onto the end of string constants; the C++ compiler does this for you automatically. Therefore, the string "Hello" will appear in memory like this:

1.2.Reading a String from the Keyboard

The easiest way to read a string entered from the keyboard is to make the array that will receive the string the target of a cin statement. For example, the following program reads a string entered by the user:
// STRING1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
            char str[80];
            cout << "Enter a string: ";
            cin >> str;
            cout << "Here is your string: ";
            cout << str;
            return 0;
}

Although this program is technically correct, there is still a problem. To see what it is,
examine the following sample run.
Enter a string: This is a test
Here is your string: This
The result is shown in next figure.

As you can see, when the program redisplays the string, it shows only the word "This", not the entire sentence that was entered. The reason for this is that the >> operator stops reading a string when the first whitespace character is encountered. Whitespace characters include spaces, tabs, and newlines. One way to solve the whitespace problem is to use another of C++’s library functions, gets( ). The general form of a gets( ) call is:
gets(array-name);
If you need your program to read a string, call gets( ) with the name of the array, without any index, as its argument. Upon return from gets( ), the array will hold the string input from the keyboard. The gets( ) function will continue to read characters until you press ENTER. The header used by gets( ) is <cstdio>. This version of the preceding program uses gets( ) to allow the entry of strings containing spaces.
// Using gets() to read a string from the keyboard.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
cout << "Enter a string: ";
gets(str); // read a string from the keyboard
cout << "Here is your string: ";
cout << str;
return 0;
}

Now, when you run the program and enter the string "This is a test", the entire sentence is read and then displayed, as this sample run shows.
Enter a string: This is a test
Here is your string: This is a test
There is one other point of interest in the preceding programs. Notice that in the cout statement, str is used directly. For reasons that will be clear after you have read a few more chapters, the name of a character array that holds a string can be used any place that a string literal can be used. Keep in mind that neither >> nor gets( ) performs any bounds checking on the array. Therefore, if the user enters a string longer than the size of the array, the array will be overwritten. This makes both methods of reading a string potentially dangerous. 
Share on Google Plus