# 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 << 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, example2;

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 = {"This is a string."};
char example2 = {"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 = {"This is a string."};
char example2 = {"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 = {"This is a string."};
char example2 = {"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 = {"This is a string."};
char example2 = {"This is another string."};

if (strncmp(example1, example2, 9) == 0)
{
cout << "The strings match." << endl;
}
else
{
cout << "The strings don't match." << endl;
}```
{"ft_lang_label":"__label__en","ft_lang_prob":0.83657104,"math_prob":0.93220764,"size":5351,"snap":"2022-27-2022-33","text_gpt3_token_len":1239,"char_repetition_ratio":0.19319244,"word_repetition_ratio":0.09857612,"special_character_ratio":0.25191554,"punctuation_ratio":0.1353167,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9811809,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-30T14:23:49Z\",\"WARC-Record-ID\":\"<urn:uuid:61dcc342-7e46-4afe-a517-bbcc7ba09149>\",\"Content-Length\":\"25026\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5b78f29a-6080-41a0-9522-1e32da1a946c>\",\"WARC-Concurrent-To\":\"<urn:uuid:365497f8-806f-4b29-bbc4-a8abbbc29653>\",\"WARC-IP-Address\":\"92.53.241.39\",\"WARC-Target-URI\":\"https://stuartsplace.com/information-technology/programming/c-plus-plus/c-plus-plus-c-style-strings\",\"WARC-Payload-Digest\":\"sha1:62742AI7XI4K4CLNLZOYDBXSGOW7U6MU\",\"WARC-Block-Digest\":\"sha1:5LMXKN2ZQ2OGKAYAAD5B5A4TIREBY3W6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103821173.44_warc_CC-MAIN-20220630122857-20220630152857-00151.warc.gz\"}"}