C Strings

As mentioned, when discussing variables, there is no string variable type. In order to store a string, an array of characters must be used. An array stores each character in the string in a separate element.

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

The above array called ‘message’ 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.

printf("%c", message[2]);

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 using the ‘%s’ format specifier, together with the name of the array, without the square brackets.

printf("%s", message);

The C Standard Library provides a number of functions to help handle strings within character arrays. Some of these are discussed below. In order to utilise these functions, the header file, ‘string.h‘, 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.

Here, the length of the string in the ‘message’ array is output to the console using a ‘printf’ statement.

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

printf("String length: %d\n", strlen(message));

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 message1[50], message2[50];

strcpy(message1, "This is a string");
strncpy(message2, "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 ‘message2’ array onto the end of the string in ‘message1’ and displays the result, which is now stored in ‘message1’.

char message1[50] = {"This is a string"};
char message2[50] = {"This is another string"};

strcat(message1, message2);

printf("%s\n", message1);

An error that could occur here is if ‘message1’ 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 message1[50] = {"This is a string"};
char message2[50] = {"This is another string"};

strncat(message1, message2, 25);

printf("%s\n", message1);

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 message1[50] = {"This is a string"};
char message2[50] = {"This is another string"};

if (strcmp(message1, message2) == 0)
{
    printf("The strings match.\n");
}
else
{
    printf("The strings don't match.\n");
}

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 message1[50] = {"This is a string"};
char message2[50] = {"This is another string"};

if (strncmp(message1, message2, 9) == 0)
{
    printf("The strings match.\n");
}
else
{
    printf("The strings don't match.\n");
}