The Java String class provides a method, concat, that concatenates or appends one String object to another, and two concatenation operators: + and +=. While C++ also provides a strcat function. However, the results are meaningless when C++ programmers use the operators despite compiling without error. Like the assignment operator, these operators operate on the C-strings' addresses, not their contents. The behavior of the C++ strcat function is very similar to Java's method or += operator.
strcat: String-Concatenation
strcat(s1, s2)
Concatenating C-Strings. The strcat function concatenates two C-strings by appending the source (the second argument) to the end of the destination (the first argument). It is essential that the destination, s1, is initialized before calling strcat. If the destination is an empty string (i.e., it does not contain any text), the program must null terminate it (see Figure 2 (b) and (c)).
The strcat function. The string-concatenate function concatenates two C-strings by appending its second argument to its first. The program passes both arguments to the function by pointer, but the second argument is const and can't be modified. The standard and Microsoft versions return the concatenated string through the first argument. Notice the space before the 'W' in s2.
The standard version also returns s1 as the return value, making the concatenated C-strings available a second way.
The Microsoft version returns an error condition: 0 when the function succeeds and non-0 when it fails.
Creating an empty C-string. This example uses s1 as an accumulator, but it accumulates text rather than summing numeric values (notice the spaces at the ends of s2 through s5). Like any accumulator, we need to initialize s1, and the most common initial value is the empty string. We must allocate sufficient memory and make it logically empty. The highlighted statement makes s1 an array of 100 character elements, implying that it can hold a C-string 99 characters long. But, it makes it an empty C-string by setting the first element, s1[0], to the null terminator. The highlighted statement is a shortcut that we can more easily understand if we replace it with two equivalent statements:
char s1[100];
s1[0] = '\0';
Possible Implementations
char* strcat(char* dest, const char* source)
{
size_t size = strlen(dest);
for (size_t i = 0; i <= strlen(source); i++)
dest[size + i] = source[i];
return dest;
}
char* strcat(char* dest, const char* source)
{
char* s = dest;
while (*(s))
s++;
while (*(s++) = *(source++))
;
return dest;
}
Two implementations of the standard strcat function. You must learn how to read and understand program code. To help develop that skill, study the two implementations of the strcpy function presented in the previous section. Study the elaborations following the functions, including the review links. Describe (to yourself) how these functions work after studying the strcpy function and the previous corresponding examples.
Your elaborations don't need to be as formal or extensive as the examples but focus on understanding the code. For each function statement, ask yourself the following elaboration questions:
What does the statement do?
How does the statement connect to the problem of concatenating strings?
How does the statement help to concatenate the strings?