C語言 字串函式的分析以及模擬實現

2021-08-14 15:16:15 字數 3607 閱讀 4125

我們先來總結一下字串函式有哪些?

我們把字串函式分為兩大類,一種是不安全的,一種是優化版本

strlen

strcat,strcmp,strcpy,strstr

strncat,strncmp,strncpy

我們為什麼會有strncat,strncmp,strncpy

這些函式呢,原因是因為strcat,strcmp,strcpy這些函式的第乙個引數的陣列大小不能保證有足夠大的空間容納,如果第乙個引數陣列並沒有開闢足夠大的記憶體空間,那麼就會導致多餘的字元溢位到相鄰的儲存單位,這樣後果是很嚴重的,所以我們進行了優化,在每個函式上新增了乙個引數n,來進行限制

下面我們來分析一下這些函式:

strlen求字串長度

函式原型:

size_t strlen ( const

char * str );

**模擬實現:

int my_strlen(const char * str)

printf("%d", count);

}

strcat連線字串

函式原型:

char * strcat ( char * destination, const

char * source )

返回值是第乙個引數的值

該函式存在不安全性,他並不檢查第乙個陣列是否能夠容納第二個陣列,如果沒有為第乙個陣列開闢足夠大的空間,就會導致多餘的字元溢位到相鄰儲存單元,就會出現問題。

**模擬實現:

char* my_strcat(char *dest, char *src)

while (*src!='\0')

*dest = '\0';

return strdest;

}int main()

strncat連線字串

函式原型:

char * strncat ( char * destination, const

char * source, size_t num );

比strcat多了乙個引數,指明最多允許新增的字元數目

如:strncat(str1,str2,5):該函式表示將str2追加到str1後面,直到加到第五個字元或遇到\0為止

模擬實現:

#include 

#include

#include

#include

char *my_strncat(char *dest, const char *src, size_t count)

while (count-- > 1)

*dest = '\0';

return ret;

}int main()

strcmp比較字串

函式原型:

int

strcmp ( const

char * s1, const

char * s2 );

返回值:

當s1 < s2時,返回負數。

當s1 = s2時,返回0.

當s1 > s2時,返回正數

用該函式比較字串時,一直比較到找到不同的相應字元,搜尋可能要進行到字串結尾處

strncmp比較字串

函式原型:

int

strncmp ( const

char * str1, const

char * str2, size_t num );

**模擬實現:

#include 

#include

#include

#include

int my_strncmp(const

char *str1, const

char *str2, int count)

str1++;

str2++;

count--;

}return *str1 - *str2;

}int main()

strcpy複製字串

函式原型:

char * strcpy ( char * destination, const

char * source );

將source字串賦值到destination中。

模擬實現

#include 

#include

#include

#include

char * my_strcpy(char * dst, const

char * src)

*dst = '\0';

return ret;

}int main()

strncpy複製字串
char * strncpy ( char * destination, const

char * source, size_t num )

模擬實現:

#include 

#include

#include

#include

char * my_strncpy(char * dst, const

char * src,size_t num)

*dst = '\0';

return ret;

}int main()

strstr查詢字串

函式原型:

char * strstr (char * str1, const

char * str2 );

在字串str1中查詢tr2子串.

返回值:

1. 返回子串str2在str1中首次出現位置的指標.

2. 如果沒有找到子串str2, 則返回null.

3. 如果子串str2為空串, 函式返回str1值.

**模擬實現:

#include 

#include

#include

#include

char * my_strstr(char * dest, const

char * src)

if (*str2 == '\0')

str2 = src;

str1 = start + 1;

}return null; //不存在返回null

}int main()

strchr查詢某字元在字串中首次出現的位置

函式原型:

char * strchr (       char * str, int character );
總結,在這裡我們需要特別注意的是,在模擬實現的時候一定要記得字串結束加『\0』,還有以上的函式只能用於字串,不能用於別的型別

c語言 模擬實現字串函式

1.strcpy實現字串的拷貝 char my strcpy char dest,const char src return temp 2.strcat實現字串的連線 char my strcat char arr,const char src while arr src return temp 3...

c語言 字串函式

c語言中的字串函式 主要是兩大類,一類是拷貝的,一類是比較的,另乙個就是其他的 拷貝的有 1.strcpy 函式原型 char strcpy char strdestination,const char strsource 函式功能 將strsource的內容拷貝到strdestination中去,...

c語言字串函式

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