溫馨提示×

C語言字符串連接函數如何使用

小億
136
2024-01-09 19:36:20
欄目: 編程語言

C語言中字符串連接函數使用的是strcat()函數,函數原型如下:

char* strcat(char* destination, const char* source);

strcat()函數將源字符串追加到目標字符串的末尾,并返回目標字符串的指針。

使用方法如下:

  1. 首先,確保目標字符串有足夠的空間來容納源字符串的內容。
  2. 調用strcat()函數,并將目標字符串作為第一個參數,源字符串作為第二個參數傳遞。
  3. 函數將會將源字符串的內容追加到目標字符串的末尾。
  4. 最后,可以通過打印目標字符串來驗證字符串連接的結果。

以下是一個簡單的示例代碼:

#include <stdio.h>
#include <string.h>

int main() {
    char destination[20] = "Hello";
    char source[] = " World!";

    strcat(destination, source);

    printf("Concatenated string: %s\n", destination);

    return 0;
}

輸出結果為:Concatenated string: Hello World!

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女