8.2.2.3. strcat

Time: 00:01:07 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides (PDF)

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

C-strings s1 and s2 are represented by a sequence of squares. s1 stores 'HELLO' and s2 stores 'EXAMPLE'. The strcat function first locates the null terminator, '\0', in s1 at location 5. It copies the 'E' from s2 to s1[5], overwriting the null terminator. It continues to copy the rest of the characters from s2, including the null terminator, to s1.

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)).
Header File:
#include <cstring>
Standard Prototype:
char* strcat(char* destination, const char* source);
Please see strcat for more information
Microsoft Prototype:
errno_t strcat_s(char* dest, size_t size, const char* source);
Returns 0 when the copy succeeds; returns non-0 on failure.
Please see strcat_s for more information

Examples

char* s1;
char s2[15] = { "Hello world" };
strcat(s1, s2);
char s1[5];
char s2[15] = { "Hello world" };
strcat(s1, s2);
(a)(b)
Common strcat errors.
  1. char* does not allocate memory to store the copied C-string, and s1 is not initialized with a null termination character.
  2. Although s1 is defined as an array, it's too small to hold all of the s2 characters. And s1 is not initialized with a null termination character.
The strcat function does require s1 to have a null termination character. See Figure 4 below.
Standard Version Microsoft Version
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char s1[100] = "HELLO";
	char* s2 = " WORLD";

	cout << strcat(s1, s2) << endl;
	cout << s1 << endl;

	return 0;
}
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char s1[100] = "HELLO";
	char* s2 = " WORLD";

	cout << strcat(s1, 100, s2) << endl;
	cout << s1 << endl;

	return 0;
}
 
Output:
HELLO WORLD
HELLO WORLD
0
HELLO WORLD
(a)(b)
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.
  1. The standard version also returns s1 as the return value, making the concatenated C-strings available a second way.
  2. The Microsoft version returns an error condition: 0 when the function succeeds and non-0 when it fails.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char s1[100] = "";
	char* s2 = "see ";
	char* s3 = "the ";
	char* s4 = "quick ";
	char* s5 = "red ";
	char* s6 = "fox";

	strcat(s1, s2);
	strcat(s1, s3);
	strcat(s1, s4);
	strcat(s1, s5);
	strcat(s1, s6);

	cout << s1 << endl;

	return 0;
}

Output:

see the quick red fox
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: "When you can snatch the pebble from my hand...," - when you can elaborate each statement, you will know that you are becoming a computer scientist.