This is the 8th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.

Strcat () function

The strcat() function is mainly used to concatenate strings. It is used to concatenate one string to the end of another string. Here is a simple example of how to use this function.

int main(a)
{
	char str1[10] ="123";
	char str2[4] ="abc";
	
	printf("str1:%s\r\n",str1);
	printf("str2:%s\r\n",str2);
	
	strcat(str1,str2);
	
	printf("str1:%s\r\n",str1);
	printf("str2:%s\r\n",str2);

	system("pause");
	return 0;
}
Copy the code

We define two strings str1 and str2, print the two strings first, then use strcat() to concatenate the str2 strings to the end of str1, and print the two strings again. The following output is displayed:You can see from the output that string 2 has indeed been concatenated to the end of string 1. Note, however, that this function does not detect the length of string 1 by default. If string 1 is not large enough for string 2, it may cause an overflow. For example, append a long string 2 to string 1 here.When the program finished running, the software had an error. This shows that when strcat() is used to append content to str1 string, due to the small space of STR1 and the large number of appended characters, the extra characters will overflow into the adjacent storage space, resulting in software crash.

Strncat () function

To solve this problem, a new string append function strncat() is provided. This function takes one more argument than strcat(), and the third argument sets the length of the append. In this way, when appending strings, you can control the length of the appended characters without overflow.

int main(a)
{
	char str1[10] ="123";
	char str2[]="abcdefghjhijkl";
	
	int cnt = 0,cnt1 = 0,cnt2 = 0;
	
	printf("str1:%s\r\n",str1);
	printf("str2:%s\r\n",str2);
	
	cnt = strlen(str1);
	cnt1 = sizeof(str1) / sizeof(str1[0]);
	cnt2 = cnt1 - cnt;
	printf("%d,%d,%d\r\n",cnt,cnt1,cnt2- 1);
	
	strncat(str1,str2,cnt2);
	
	printf("str1:%s\r\n",str1);
	printf("str2:%s\r\n",str2);

	system("pause");
	return 0;
}
Copy the code

We print the original contents of string 1 and string 2, then use strlen() to calculate the actual sizeof string 1, sizeof() to calculate the total sizeof the string, and then calculate the difference between the two, which is the remaining space in string 1. When using strncat() to concatenate strings, the last argument is the amount of space left in string 1. This will not cause data overflow when concatenating strings.

It can be seen from the printed result that the last concatenated character of string 1 after concatenation only truncates part of string 2, but does not concatenate all the content of string 2. The concatenated string is 6 in length. The remaining length of the string is calculated to be 7, but it also takes into account the null character ‘\0’ at the end of the string. So when strncat() is used, the length of the last argument is reduced by one, leaving a blank character for string 1.