8.2.2.3. strcat

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

Joining or concatenating two strings is another fundamental string operation, and the C-string library implements the operation with the strcat function. strcat is similar to the strcpy function, but requires offsetting the destination index during the copy operation.

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 parameter) to the end of the destination (the first parameter). It is essential that the destination, s1, is initialized before calling strcat and refers to a contiguous block of memory large enough to hold both strings. If the destination is an empty string (i.e., it does not contain any text), the program must null-terminate it before the concatenation operation.
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

strcat Examples

char* s1;
char s2[15] = { "Hello world" };
strcat(s1, s2);
char s1[5] = "";
char s2[15] = { "Hello world" };
strcat(s1, s2);
char s1[15];
char s2[15] = { "Hello world" };
strcat(s1, s2);
(a)(b)(c)
Common strcat errors. The destination string, s1, must satisfy two requirements: First, it must be null-terminated before the function call. Second, it must point to sufficient memory to hold both strings plus a single null-termination character.
  1. Although the definition of s1 matches the strcat prototype, it neither points to sufficient memory, nor is it null-terminated.
  2. s1 is an empty, null-terminated array, but it is too small to hold all of the s2 characters.
  3. s1 is sufficiently large to hold all the concatenated characters, but it is not null-terminated.

 

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, the program must initialize s1, and the most common initial value is the empty string. It must also have sufficient memory hold all the concatenated characters. The highlighted statement makes s1 an array of 100 characters, implying that it can hold a C-string 99 characters long. Assigning an empty string literal to s1 sets the first element, s1[0], to the null terminator. The single highlighted statement is equivalent to the statements:
char s1[100];
s1[0] = '\0';

Possible strcat 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))
		;     

	while (*(s++) = *(source++))
		;

	return dest;
}
(a)(b)
Two implementations of the standard strcat function. Except for calculating an offset (highlighted), the implementation of the strcat function is identical to strcpy described in the previous section. Please see that example for a full description of the features in common with this example. As an aid for following the concatenation code, imagine that s1 and s2 are as illustrated. The digits are the character indexes, the magenta boxes are the null-termination characters, and the green box is the space at the end of s1.
char s1[100] = "Hello ";
char s2[100] = "world";
 
    0123456
s1: Hello  
s2: world 
  1. The strlen function returns the length of s1: six, counting the space but not the null-termination character. The for-loop iterates i from 0 through 6 (note the <= operator). Therefore, the assignment operation begins at dest[6] and source[0]. size
  2. The first while-loop creates an offset pointer by locating the end of dest by incrementing the pointer s until it identifies the null-termination character. The second loop copies the characters from source to the end of dest. Why does the first loop use the pre-increment operator while the second uses the post-increment operator? Programmers can replace the highlighted loop with:
    while (*s)
    	s++;