C語言中的strcat()函式是什麼?


C庫函式 **char *strcat(char *dest, const char *src)** 將由 **src** 指向的字串附加到由 **dest** 指向的字串的末尾。

字元陣列稱為字串。

宣告

以下是陣列的宣告:

char stringname [size];

例如:char string[50]; 長度為50個字元的字串

初始化

  • 使用單個字元常量:
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
  • 使用字串常量:
char string[10] = "Hello":;

**訪問**:使用控制字串“%s”訪問字串,直到遇到‘\0’。

strcat() 函式

  • 用於組合或連線兩個字串。

  • 目標字串的長度必須大於源字串。

  • 結果連線字串為源字串。

語法

語法如下:

strcat (Destination String, Source string);

示例程式

以下程式演示了strcat()函式的使用。

 線上演示

#include <string.h>
main(){
   char a[50] = "Hello 
";    char b[20] = "Good Morning
";    strcat (a,b);    printf("concatenated string = %s", a); }

輸出

執行上述程式後,將產生以下結果:

Concatenated string = Hello Good Morning

示例

讓我們看另一個例子。

以下是使用strcat庫函式將源字串連線到目標字串的C程式:

#include<stdio.h>
#include<string.h>
void main(){
   //Declaring source and destination strings//
   char source[45],destination[50];
   //Reading source string and destination string from user//
   printf("Enter the source string : 
");    gets(source);    printf("Enter the destination string :
");    gets(destination);    //Concatenate all the above results//    strcat(source,destination);    //Printing destination string//    printf("The modified destination string :");    puts(source); }

輸出

執行上述程式後,將產生以下結果:

Enter the source string :Tutorials Point
Enter the destination string :C programming
The modified destination string :Tutorials Point C programming

更新於:2021年3月19日

1K+ 瀏覽量

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告