Concatenating Strings in C
A common operation that often occurs when working with strings is the concatenation of two strings. The C language does not provide an operator to concatenate two strings as many other languages do.
However, we can use the strcat and strncat functions from the standard library string.h to concatenate two strings in C language.
They accept two string pointers and concatenate the second string to the end of the first. Additionally, they return the pointer to the first string.
In this lesson we will see how to use the strcat and strncat functions to concatenate two strings in C language. Furthermore, we will see how to manually implement these functions to better understand their operation and intrinsic problems.
Function strcat - String concatenation
In C language we cannot use the addition operator +, as many other languages do, to concatenate two strings.
We can, instead, use the strcat function from the standard library string.h.
The strcat function accepts two string pointers and concatenates the second string to the end of the first. Additionally, it returns the pointer to the first string.
Its signature is:
char *strcat(char *dest, const char *src);
Where:
destis the pointer to the destination string, that is the string to which we want to add the second string.srcis the pointer to the source string, that is the string we want to add to the destination string.
Let's see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
In this example, we define two character arrays s and t and initialize them with the strings "Hello" and "World". Then, we concatenate the string t to the string s using the strcat function. Finally, we print the result.
The program prints:
First: Hello
Second: World
After: HelloWorld
As we can see, the string t has been concatenated to the string s.
The return value of the strcat function is often ignored, since it returns the pointer to the destination string, which is the same pointer passed as the first argument. However, we can use it to concatenate multiple strings in a single line of code:
char s[128] = "Hello";
char t[128] = "World";
char u[128] = "!";
strcat(s, strcat(t, u));
In this case, the string u is concatenated to the string t; subsequently, the result of this operation, namely t, is concatenated to the string s.
The final result is:
uwill contain"!";twill contain"World!";swill contain"HelloWorld!".
Function strcat
The strcat function accepts two string pointers and concatenates the second string to the end of the first. Additionally, it returns the pointer to the first string.
It is defined in the standard library string.h:
#include <string.h>
Its signature is:
char *strcat(char *dest, const char *src);
destis the pointer to the destination string, that is the string to which we want to add the second string.srcis the pointer to the source string, that is the string we want to add to the destination string.
The return value is the pointer to the destination string: dest.
The strcat function is not safe
The strcat function does not verify if the destination string has enough space to contain itself and the source string. If the destination string does not have enough space, the function can overwrite adjacent memory, causing undefined behaviors.
To avoid this problem, we can use the strncat function, which accepts a third argument that specifies the maximum number of characters to concatenate.
Function strncat - String concatenation with limit
To avoid the problem of strcat, we can use the strncat function from the standard library string.h.
The strncat function accepts two string pointers and an upper limit and concatenates the second string to the end of the first, up to the upper limit. Additionally, it returns the pointer to the first string.
Its signature is:
char *strncat(char *dest, const char *src, size_t n);
Where:
destis the pointer to the destination string, that is the string to which we want to add the second string.srcis the pointer to the source string, that is the string we want to add to the destination string.nis the maximum number of characters to concatenate, excluding the terminator\0.
Let's see an example of usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
In this example, we define two character arrays s1 and s2 and initialize them with the strings "Hello" and "World".
Then, we concatenate the string s2 to the string s1 using the strncat function. The third argument passed to the function is the maximum number of characters to concatenate, which is calculated as the difference between the maximum size of the array s1, MAX_SIZE, and the current length of the string s1, obtained with the strlen function, minus 1 for the terminator \0.
The program output will be:
First: Hello
Second: World
After: HelloWorld
Calculating the number of characters to concatenate
If we need to concatenate two strings, s and t, and we know that the destination string, s, has a maximum size of MAX_SIZE, we can calculate the number of characters to concatenate as:
MAX_SIZE - strlen(s) - 1
This is because to the destination string we can add a number of characters equal to its maximum length, minus its current length, minus 1 for the terminator \0.
Therefore, the typical usage of strncat is:
strncat(s, t, MAX_SIZE - strlen(s) - 1);
Implementation of strcat and strncat
Also in this case, it is useful, from an educational point of view, to implement the strcat and strncat functions manually. In this way we can better understand their operation and intrinsic problems.
Let's start with the strcat function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Let's analyze the code. The function uses a local pointer, p, with which it scans the destination string. In particular, it uses two while loops:
- The first
whileloop, lines 4-6, scans the destination string until it finds the terminator\0; - After this
whileloop we have that the pointerppoints to the terminator\0of the destination string. This terminator will be replaced with the first character of the source string,src. To do this, we use a secondwhileloop, lines 8-12, which copies the characters of the source string into the destination string.
Finally, the function places the terminator \0 at the end of the destination string and returns the pointer to it.
The problem with strcat lies precisely in the fact that the two while loops do not check if the destination string has enough space to contain itself and the source string. Not only that, but they also do not check if the two string pointers passed as arguments point to valid strings.
The strncat function solves the first problem. Let's see a possible implementation that we will call my_strncat:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
The my_strncat function is similar to my_strcat, but has a third argument, n, which represents the maximum number of characters to concatenate. The main difference compared to strcat lies in the second while loop, lines 8-13, which also checks the number of characters to concatenate.
In fact, the condition *src != '\0' && n > 0 checks if the current character of the source string is different from the terminator \0 and if the number of characters to concatenate is greater than 0. Furthermore, inside the while loop, we decrement the counter n at each iteration.
In this way, the my_strncat function will not concatenate more than n characters, thus avoiding overwriting adjacent memory.
In Summary
In C language, we can concatenate two strings using the strcat function from the standard library string.h. This function accepts two string pointers and concatenates the second string to the end of the first. However, the strcat function does not verify if the destination string has enough space to contain itself and the source string, causing undefined behaviors.
To avoid this problem, we can use the strncat function, which accepts a third argument that specifies the maximum number of characters to concatenate.
Both functions return the pointer to the destination string.
Furthermore, we have seen how to manually implement the strcat and strncat functions, to better understand their operation and intrinsic problems.
In the next lesson we will see how to compare strings in C language.