模擬實現strncat

2021-07-12 07:14:09 字數 689 閱讀 4222

模擬實現strncat:

在系統庫函式中,存在strncat這個函式,它用於字串的追加,就是在乙個字串後面再追加乙個字串,它的函式原型為:

char *strncat( char *strdest, const char *strsource, size_t count );
在其中,*strdest為目標字串,*strsource為源字串,count為需要追加的字串的個數,strncat就是將源字串追加count個字元在目標字串後面。size_t表示無符號整型,因為count不可能為負數。同樣也可以自己定義:

typedef unsigned int nuit;
模擬**實現 :

#include#include#includetypedef unsigned int uint;

char *my_strncat(char *dest, const char *src, uint count)//模擬實現strncat函式

while (count--)

*dest = '\0';

return ret;

}//程式測試

int main()

本文出自 「stand out or get out」 部落格,請務必保留此出處

模擬實現strncat

先演示一下,strncat函式的效果,程式 如下 include include void main void 執行結果如下 函式引數有原字串,目標字串,操作字元數,函式執行後會將一定數目的目標字串內容加到原字串的後面,下面給出程式 include include include include c...

模擬實現strncat

需要注意幾點 strncat最多從源字串複製len個字元到目標字元陣列 strncat總是在結果後面新增 0 而不是用 0 填充目標剩餘空間 程式 如下 define crt secure no warnings include include include char mystrncat char...

c語言模擬實現strncat

在c語言的庫函式中,strcat負責將乙個字串加在另乙個字串的後面,但他只能將乙個字串的所有字元加在另一字串的後面,而strncat則可以選擇性的在字串後加字串,使追加字串更靈活,更安全。在選擇性的追加字串前,要先知道兩個字串的長度,且被追加的字串的後面有足夠的空間來接收所追加的字串,所以被追加字串...