C語言模擬實現庫函數詳解
2022-07-13 14:03:13
前言
庫函數的模擬,看似多此一舉,實則汲取精華。
1.字串函數
1.1字串控制函數
1.1.1 strlen的模擬
求字串長度
//-------------------------------------- size_t strlen( const char *string ); //-------------------------------------- //返回值用 int / size_t 各有好處 int MyStrlen(const char* str) { assert(str); int cnt = 0; while (*str != ' ') { str++; cnt++; } return cnt; } int main() { char arr[] = "bacon"; printf("%dn", MyStrlen(arr)); return 0; }
1.1.2 str(n)cpy的模擬
拷貝字串
//------------------------------------- char* strcpy(char* dest, const char* src) //------------------------------------- char* MyStrcpy(char* dest, const char* src) { assert(dest && src); char* ret = dest; while (*dest++ = *src++); return ret; } int main() { char str1[30] = "Bacon"; char str2[] = " is your friend."; MyStrcpy(str1 + 5, str2); printf("%sn", str1); return 0; } //Bacon is your friend. //------------------------------------------------------------ char* strcpy(char* dest, const char* src, size_t num) //--------------------------------------------------------------- char* MyStrncpy(char* dest, const char* src, size_t num) { assert(dest && src); char* ret = dest; while (num--) { *dest++ = *src++; } return ret; } int main() { char str1[20] = { 0 }; char str2[] = "bacon"; printf("%sn", MyStrncpy(str1, str2, 4)); return 0; } //baco
Bacon is your friend.
baco
1.1.3 str(n)cmp的模擬
逐個比較字串中的字元
//----------------------------------------------- int strcmp(const char* str1, const char* str2) //----------------------------------------------- int MyStrcmp(const char* str1, const char* str2) { assert(str1 && str2); while (*str1 != ' ' && *str2 != ' ' && *str1 == *str2) { str1++; str2++; } if (*str1 == *str2) return 0; else if (*str1 > *str2) return 1; else return -1; } int main() { char str1[] = "abcdef"; char str2[] = "abcdfe"; printf("%dn", MyStrcmp(str1, str2)); return 0; } //-1 //----------------------------------------------------- int strncmp(const char* str1, const char* str2, size_t num) //--------------------------------------------------------- int MyStrncmp(const char* str1, const char* str1, size_t num) { assert(dest && src); while (num--) { if (*str1!= *str2) { if (*str1> *str2) return 1; else return -1; } str1++; str2++; } return 0; } int main() { char str1[] = "abcdef"; char str2[] = "abcdfe"; printf("%dn", MyStrncmp(str1, str2, 5)); return 0; }
-1
1.1.4 str(n)cat的模擬
字串追加
//---------------------------------------------- char* strcat(char* dest, const chat* src) //---------------------------------------------- char* MyStrcat(char* dest, const char* src) { assert(dest && src); char* ret = dest; //找到字串結尾 while (*dest != ' ') { dest++; } while (*dest++ = *src++); return ret; } int main() { char str1[20] = "bacon"; char str2[] = " king"; printf("%sn", MyStrcat(str1 , str2)); return 0; } //bacon king //---------------------------------------------------------- char* strncat(char* dest, const char* src, size_t num); //---------------------------------------------------------- char* MyStrncat(char* dest, const char* src, size_t num) { assert(dest && src); char* ret = dest; while(*dest != ' ') { dest++; } while (num--) { *dest++ = *src++; } return ret; } int main() { char str1[30] = "Bacon"; char str2[] = " is your friend"; printf("%sn", MyStrncat(str1, str2, 8)); return 0; } //Bacon is your
bacon king
Bacon is your
1.1.5 strstr的模擬
查詢子串
//------------------------------------------------ char* strstr(const char* str1, const char* str2) //-------------------------------------------------- char* MyStrstr(const char* str1, const char* str2) { assert(str1 && str2); char* p = str1; char* s1 = str1; char* s2 = str2; while (*p)//str1走完就可以跳出了 { //找到可能匹配的位置 while (*s1 != *s2) { s1++; } p = s1; while (*s1 != ' ' && *s2 != ' ' && *s1 == *s2) { s1++; s2++; } if (*s2 == ' ') { return p; } else { s1 = p + 1; s2 = str2; p = s1; } } return NULL; } int main() { char str1[] = "abbbcdef"; char str2[] = "bbc"; printf("%sn", MyStrstr(str1, str2)); return 0; }
1.1.6 strtok的使用
切分字串:根據給定的分隔符,把分隔符置 ‘