strncat()在C++中的用法


C++中的函式strncat()用於串聯。它將源字串中指定數量的字元附加到目標字串的末尾,並返回一個指向目標字串的指標。strncat()的語法如下。

char * strncat ( char * dest, const char * src, size_t num );

在上面的語法中,源字串src將被附加在目標字串dest的最後,僅限於num個字元。

一個演示strcat()的程式如下。

示例

 線上演示

#include <iostream>
#include <cstring>
using namespace std;
int main() {
   char str1[20] = "Programming is ";
   char str2[20] = "fun";
   strncat(str1, str2, 3);
   cout << "The concatenated string is "<<str1;
   return 0;
}

輸出

The concatenated string is Programming is fun

在上面的程式中,定義了兩個字串str1和str2。strncat()將str2的內容附加在str1的末尾,直到三個字元,然後使用cout顯示串聯字串。這如下所示。

char str1[20] = "Programming is ";
char str2[20] = "fun";
strncat(str1, str2, 3);
cout << "The concatenated string is "<<str1;

更新於: 24-06-2020

478次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.