1. 程式人生 > >C語言拼接字串 -- 使用strcat()函式

C語言拼接字串 -- 使用strcat()函式

【標頭檔案】#include <string.h>

【原型】

1

char *strcat(char *dest, const char *src);

【引數】: dest 為目標字串指標,src 為源字串指標。

strcat() 會將引數 src 字串複製到引數 dest 所指的字串尾部;dest 最後的結束字元 NULL 會被覆蓋掉,並在連線後的字串的尾部再增加一個 NULL。

【注意】 dest 與 src 所指的記憶體空間不能重疊,且 dest 要有足夠的空間來容納要複製的字串

【返回值】 返回dest 字串起始地址。

【例項】連線字串並輸出。

1

2

3

4

5

6

7

8

9

10

11

12

#include <stdio.h>

#include <string.h>

int main ()

{

char str[80];

strcpy (str,"these ");

strcat (str,"strings ");

strcat (str,"are ");

strcat (str,"concatenated.");

puts

 (str);

return 0;

}

輸出結果:
these strings are concatenated.