We all know that strcat and strncat are both functions that append characters to a string. What is the difference between them? What are their principles?
First of all, we must understand the principles of the two to be able to distinguish between them;
the usage of strcat-call the strcat function, pass two string arrays or a string array and a string, you need to call the string.h header file ;
char arr1[30] = "abcd";
char arr2[] = "efd";
strcat(arr1, arr2);
----------------------
strcat (str,"concatenated.")
The source code is (I implemented it myself, it will be different from the real source code, but the principle is the same)
Step 1: Point the pointer of the added string to the string'\0'
Step 2: Change Exchange the added character string with the'\0' of the added character string.
Step 3: Set the last position of str to'\0'
void strcat(char* str, const char* d)
{
while (*str)
str++;
while (*d != '\0')
{
*str = *d;
str++;
d++;
}
str++;
*str = '\0';
}
int main()
{
char arr1[30] = "abcd";
char arr2[] = "efd";
strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
Usage of strncat-call the strncat function, pass two strings and an integer value, the integer value represents the number of characters to be appended, you need to call the string.h header file;
char str1[20] = "abcde";
char str2[20] = "cdefad";
strncat (str1, str2, 6);
Source code:
Step 1: Move the pointer of arr1 to'\0'.
Step 2: Exchange the element pointed to by arr2 with the element pointed to by arr1;
void my_strncat(char* arr1, char* arr2, int len)
{
assert(arr1);
assert(arr2);
arr1 = arr1 + len;
while (len-- != 0)
{
//第一步:先把arr1的指针移到'\0'处
//第二步:把arr2所指向的元素与arr1所指向的元素交换;
*arr1 = *arr2;
arr1++;
arr2++;
}
}
int main()
{
char arr1[30] = "abcdeef";
int len = strlen(arr1);
my_strncat(arr1, arr1, len);
printf("%s\n", arr1);
return 0;
}
It can be clearly seen that the parameters of the two are inconsistent, but the effect is the same. The main difference between the two is that the end condition of the added string of strcat() is str2 =='\0' (str2 is required The added string); the condition for the end of the loop of strncat is that the number of strings to be appended len is 0;
So why should there be two functions that achieve the same effect?
It seems that the implementation effect of the two is the same, but if you want to add the original string to the original string, you can only use the strncat function;
char arr1[30] = "abcd";
strcat(arr1, arr1);
Because strcat essentially exchanges the'\0' of the string to be added with the element of the added string, then when the addition is itself,'\0' will be exchanged with the previous element, when all exchanges are completed Later, you will find that'\0' has gone to the back again, so that the termination condition will no longer be established, and you will fall into an infinite loop; the convenience of strcat() is that it can be implemented only by passing two parameters, which is relatively simple;
However, it is recommended to use strncat(), because the above-mentioned possibility of falling into an infinite loop will not occur. At most, one more step length (strlen(array name)) is counted, and strncat() has an indispensable advantage, which is freedom Control the characters to be added;