C++ C-Style Strings

In the C programming language there is no string variable type. In order to store a string, an array of characters must be used, with each character stored in a separate element of the array. Although C++ has a string variable type, it supports the C-style strings as well.

char example[] = {"This is a string."};

The above array called ‘example’ is assigned the string, ‘This is a string’. One important thing to note with an array containing a string is that the number of elements will always be one more than the number of characters in the string. This is because a null character, ‘\0’, is automatically added to the end. If the size of the array is explicitly specified in the square brackets, this needs to be taken in to account to ensure that the desired string will fit into the array.

As with any type of array, each element can be accessed using its index value.

cout << example[2] << endl;

Here, the third element in the array, with the index value of two, is displayed in the console, which in this case is the character ‘i’.

In order to display the whole string, it is simply a case of specifying the name of the array, without the square brackets.

cout << example << endl;

The C++ Standard Library provides a number of functions to help handle C-style strings within character arrays. Some of these are discussed below. In order to utilise these functions, the header file, ‘cstring‘, must be included at the top of the program.

String Length

If it is necessary to find the length of a string, the function, ‘strlen’ can be used. This is a simple function that takes one argument, the character array containing the string in question. It should be noted that the null character, ‘\0’, is not included in the result returned.

char example[] = {"This is a string."};

cout << "String length: " << strlen(example) << endl;

Copying Strings

If a character array is not initialised with a string when it is defined, then the ‘=’ sign cannot be used to assign a string at a later stage in a program. Instead the string must be copied into the array using one of two functions, ‘strcpy’ or ‘strncpy’.

char example1[50], example2[50];

strcpy(example1, "This is a string.");
strncpy(example2, "This is another string.", 50);

The function ‘strcpy’ takes two arguments, the name of the array that the string needs to be assigned to and the string itself. A problem that could arise here is if the string is too long for the array. If this occurs, then an error will be produced. The second function, ‘strncopy’, takes a third argument, which is the maximum number of characters of the string to copy into the array. This will alleviate the possibility of an error occurring, assuming that this third argument isn’t set to a value higher than the length of the array.

Concatenating Strings

It is possible to concatenate a string stored in one character array with another, using either of the functions, ‘strcat’ or ‘strncat’. The ‘strcat’ function takes two arguments, the two character arrays containing the strings to be combined, and concatenates the string contained in the second array on to the end of the string in the first. The example below concatenates the string in the ‘example2’ array onto the end of the string in ‘example1’ and displays the result, which is now stored in ‘example1’.

char example1[50] = {"This is a string."};
char example2[50] = {"This is another string."};

strcat(example1, example2);

cout << example1 << endl;

An error that could occur here is if ‘example1’ is not large enough to hold the combined strings. To help with this issue, the second function that can be used to concatenate strings, ‘strncat’, requires a third argument, which limits the number of characters concatenated on to the first string from the second.

char example1[50] = {"This is a string."};
char example2[50] = {"This is another string."};

strncat(example1, example2, 25);

cout << example1 << endl;

Comparing Strings

Comparing strings is sometimes necessary, for example, to make a decision in an ‘if’ statement. Unlike with other types of variables, it isn’t possible to use ‘==’ to compare two strings in an ‘if’ statement, however, one of two function can be used instead, ‘strcmp’ or ‘strncmp’. The first of these compares the strings, character by character, until it finds characters that differ and returns an integer value as a result. If a zero is returned, then the strings match, otherwise the strings don’t match.

char example1[50] = {"This is a string."};
char example2[50] = {"This is another string."};

if (strcmp(example1, example2) == 0)
{
    cout << "The strings match." << endl;
}
else
{
    cout << "The strings don't match." << endl;
}

In the example, the strings don’t match, so the second message, “The strings don’t match”, will be displayed in the console.

The function ‘strncmp’ allows for the comparison of a limited portion of the two strings. It has a third argument where the number of characters to compare can be specified.

Here the first nine characters of the two strings are compared and this time the message, “The strings match” is displayed in the console.

char example1[50] = {"This is a string."};
char example2[50] = {"This is another string."};

if (strncmp(example1, example2, 9) == 0)
{
    cout << "The strings match." << endl;
}
else
{
    cout << "The strings don't match." << endl;
}