c语言中strcat函数_C和C ++中的strcat()函数教程及示例

c语言中strcat函数

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 thedst定义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”是字符计数,可以是字节,整数或长整数。
LEARN MORE  Append() Function Examples For Python, JavaScript, and jQuery
了解更多适用于Python,JavaScript和jQuery的Append()函数示例

翻译自: https://www.poftut.com/strcat-function-tutorial-in-c-and-cpp-with-examples/

c语言中strcat函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值