Strings

In all programs seen until now, we have used only numerical variables, used to express numbers exclusively. But in addition to numerical variables there also exist strings of characters, that allow us to represent successions of characters, like words, sentences, names, texts, et cetera. Until now we have only used them as constants, but we have never considered variables able to contain them.

In C++ there is no specific elemental variable type to store strings of characters. In order to fulfill this feature we can use arrays of type char, which are successions of char elements. Remember that this data type (char) is the one used to store a single character, for that reason arrays of them are generally used to make strings of single characters.

C++ does not support strings as a built-in data type. It rather implements strings as single-dimensional character arrays.

For example to declare an array name that holds a 20-character string,

char name[21];

This makes room for the null character at the end of the string.

For example:

0 1 2 3 4 5 6 7 8

The array above can be declared as

char home[9];

This maximum size of 20 characters is not required to always be fully used. For example, string message could store at some moment in a program either the string of characters "Hello" or the string "Merry christmas". Therefore, since the array of characters can store shorter strings than its total length, a convention has been reached to end the valid content of a string with a null character, whose constant can be written 0 or '\0'.

For example, the following array (or string of characters);

char message[20];

can store a string with a maximal length of 20 characters.

We could represent message (an array of 20 elements of type char) storing the strings of characters "Hello" and "Merry Christmas" in the following way:

Note how after the valid content a null character ('\0') it is included in order to indicate the end of the string. The panels in gray color represent indeterminate values.

Initialization of Strings

Strings of characters are ordinary arrays and the rules of arrays apply to them also. For example, a string of characters can be initilized with predetermined values, as any other array is:

char str[]={'H','e', 'l', 'l', 'o', '\0'};

Constants that represented entire strings of characters. these are specified between double quotes, for example:

"my name is; "

is a constant string.

Unlike single quotes (' ) which allow to specify single chatacter constants, double quotes ( " ) are constants that specify a succession of characters and additionally every double quoted string is automatically appended with a null character('\0') at the end.

Therefore we could initialize the string str with values by:

char str[]={'H','e', 'l', 'l', 'o', '\0'};

or char str[]="Hello";

Array of Strings

A two dimentional array of characters can be considered as an array of strings. The row subscript decides as to how many strings can be stored in the array and the column subscript tells us the size of each string.

For example:

char name[50][30];

can hold 50 strings of size 30;

Here, [50] is the row subscript and [30] is the column subscript.

HOME LEARN C++ PREVIOUS NEXT