溫馨提示×

c語言如何把字符串連接起來

小億
108
2024-01-27 13:53:16
欄目: 編程語言

在C語言中,可以使用strcat函數(shù)來將字符串連接起來。strcat函數(shù)的原型為:

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

其中,destination為目標(biāo)字符串,source為要連接的字符串。

下面是一個示例代碼:

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

int main() {
    char str1[100] = "Hello";
    char str2[] = " World!";
    
    strcat(str1, str2);
    
    printf("連接后的字符串:%s\n", str1);
    
    return 0;
}

在該示例中,str1為目標(biāo)字符串,str2為要連接的字符串。通過調(diào)用strcat函數(shù)將str2連接到str1上,最終輸出的結(jié)果為"Hello World!"。

0