c語言 字串函式

2021-05-31 22:48:56 字數 2984 閱讀 6304

c語言中的字串函式

主要是兩大類,一類是拷貝的,一類是比較的,另乙個就是其他的

拷貝的有:

1. strcpy

函式原型

char *strcpy( char *strdestination, const char *strsource );

函式功能

將strsource的內容拷貝到strdestination中去,包括' \0'

返回值返回strdestination的內容

特點strsource和strdestination的記憶體區域可能重疊,沒有定義

沒有檢查strdestination的大小是否夠

2 strcpy_s

函式原型

errno_t strcpy_s(

char *strdestination,

size_t numberofelements,

const char *strsource

);

函式功能

將strsource的內容拷貝到strdestination中去,包括' \0'

返回值成功返回0

特點strsource和strdestination的記憶體區域可能重疊,沒有定義

檢查了strdestination的大小

3 memset

函式原型

void *memset(   void*dest

,    intc

,    size_tcount

);

函式功能

將count個c 賦值到dest裡面去,經常用於將字串清零。

返回值dest

4 memcpy

函式原型

void*memcpy(    void*dest

,    const void*src

,    size_tcount

);

函式功能

將count個字元從src中拷貝到dest中

返回值

dest特點

**原型

char* m_memcpy(char* dest, const char* src, int n)

*p='\0';

return dest;

}

5  memmove

函式原型

void *memmove(    void*dest

,    const void*src

, size_tcount

);

函式功能

將count個字元從src中拷貝到dest中

返回值

dest特點

處理的方式就是判斷有沒有重疊的部分,有的話就從後往前拷貝

**原型

char* m_memmove(char* dest, const char* src, int n)

{ char* p=dest;

if (src+n>p && src

字串比較函式

1 strcmp

函式原型

int strcmp( const char*string1

,const char*string2

);

特點

不忽略大小寫

2 strncmp

函式原型

int strncmp( const char*string1

,const char*string2

,size_tcount

);

特點

不忽略大小寫

3 memcmp

函式原型

int memcmp(    const void*buf1

,    const void*buf2

,    size_tcount

);

特點同strncmp差不多,但是具體的差別還需要進一步研究呀

int r = strncmp("1234","1234",7);  //字串為0退出,迴圈4次

int r2 = memcmp("1234","1234",7);  //迴圈7次或者遇到不相等的時候退出。所以當比較的數大於字串長度時候,不安全啊。

也可以理解,strncmp是字串比較,所以可以用\0來判斷,但是void*的就不好判斷了。

4 strnicmp

函式原型

int strnicmp(

const char *string1,

const char *string2,

size_t count

);

特點

不區分大小寫的比較

c語言字串函式

字串連線函式strcat 格式 strcat 字元陣列1,字元陣列2 功能 把字元陣列2連到字元陣列1後面 返值 返回字元陣列1的首位址 說明 字元陣列1必須足夠大 連線前,兩串均以 0 結束 連線後,串1的 0 取消,新串最後加 0 字串拷貝函式strcpy 格式 strcpy 字元陣列1,字串2...

C語言字串函式

1字串拷貝 strcpy str1,str2 str2 str1覆蓋 strncpy str1,str2,n 把str2的n個位元組賦給str1的前n個位元組,其他的不變 並不覆蓋 例子 str1 yaomingyue str2 kkkk strncpy str1,str2,3 結果 str1 kk...

c語言字串函式

函式 說明atof 將字串轉換成浮點數 atoi 將字串轉換成整數 atol 將字串轉換成長整型數 gcvt 將浮點型數轉換為字串 四捨五入 strtod 將字串轉換成浮點數 strtol 將字串轉換成長整型數 strtoul 將字串轉換成無符號長整型數 index 查詢字串並返回首次出現的位置 r...