c语言中strcat函数
strcat() function is mainly used to copy or add string or char arrays in C and C++ programming languages. In general strcat() function will copy or add given string or char array into the destination string or char array.
strcat()函数主要用于以C和C ++编程语言复制或添加字符串或char数组。 通常,strcat()函数会将给定的字符串或char数组复制或添加到目标字符串或char数组中。
strcat()函数语法 (strcat() Function Syntax)
The strcat()
function has the following syntax. The strcat()
function is provided by <string.h>
library or header.
strcat()
函数具有以下语法。 strcat()
函数由<string.h>
库或标头。
char *strcat(char *DESTINATION, const char *SOURCE);
- `char *strcat` is the function where it will return a char pointer. This returns a pointer to the DESTINATION. char * strcat是函数,它将返回一个char指针。 这将返回一个指向DESTINATION的指针。
- `char *DESTINATION` is the character array or string where the SOURCE will be copied or added. “ char * DESTINATION”是将在其中复制或添加源的字符数组或字符串。
- `const char *SOURCE` is the character array or string which will be copied or added into DESTINATION. const char * SOURCE是要复制或添加到DESTINATION中的字符数组或字符串。
使用strcat()函数复制字符串 (Copy String with strcat() Function)
We will start with a simple example where we will copy the character array str
into the dst
string. In this example, we will copy or add the Poftut.com
into the end of the I love the
string which is defined with dst
. Then we will print the dst
variable with the puts()
function.
我们将从一个简单的示例开始,将字符数组str
复制到dst
字符串中。 在此示例中,我们将Poftut.com
复制或添加到I love the
用dst
定义I love the
字符串的末尾。 然后,我们将使用puts()
函数打印dst
变量。
/* strcat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char dst[20]="I love the ";
char src[20]="Poftut.com";
strcat(dst,src);
puts(dst);
return 0;
}
strcat()与strncat() (strcat() vs strncat())
There is also a similar function named strncat()
which will copy or add the specified number of characters from the given string. Let’s compare the syntax where we will also provide the number of the characters in the strncat()
function.
还有一个名为strncat()
的类似函数,该函数将从给定字符串中复制或添加指定数量的字符。 让我们比较一下语法,我们还将在strncat()
函数中提供字符数。
char *strncat(char *dest, const char *src, size_t n);
- `char *strncat` is the function where it will return a char pointer. char * strncat是函数,它将返回一个char指针。
- `char *DESTINATION` is the character array or string where the SOURCE will be copied or added. “ char * DESTINATION”是将在其中复制或添加源的字符数组或字符串。
- `const char *SOURCE` is the character array or string which will be copied or added into DESTINATION. const char * SOURCE是要复制或添加到DESTINATION中的字符数组或字符串。
- `size_t n` is the count of the characters where it can be a byte, integer or long number. “ size_t n”是字符计数,可以是字节,整数或长整数。
翻译自: https://www.poftut.com/strcat-function-tutorial-in-c-and-cpp-with-examples/
c语言中strcat函数