函数原型
char * strcat ( char * destination, const char * source );
Concatenate strings连接字符串
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.
将source的字符串连接到destination上,最后的null被覆盖,然后再最后加个null字符。
Parameters
-
destination
- Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string. source
- C string to be appended. This should not overlap destination.
/* strcat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
return 0;
}
输出结果
these strings are concatenated.
strncat 指定长度的链接字符串。
Append characters from string
Appends the first num characters of source to destination, plus a terminating null-character. If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
Parameters
-
destination
- Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string, including the additional null-character. source
- C string to be appended. num
- Maximum number of characters to be appended.
Return Value
destination is returned.
/* strncat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strncat (str1, str2, 6);
puts (str1);
return 0;
}
Output:
To be or not |