自己編寫的string庫函式

2021-04-30 00:30:58 字數 3072 閱讀 7347

//都是自己編寫的,僅供學習參考。

date: 2009.6.24

author: summon

function: functions for string.h

version: v1.0

right: all right opened

char *mystrcpy(char *pchdest, const char *pchsour)                                         

char *ret = pchdest;

dowhile (*pchsour != '/0');

*pchdest = '/0';

return ret;

}char *mystrncpy(char *pchdest, const char *pchsour, size_t count)

if (count == 0)

char *ret = pchdest;

while (count--)

}*pchdest = '/0';

return ret;

}int mystrcmp(const char *pchstr1, const char *pchstr2)

int ret = 0;

while (*pchstr1 == *pchstr2)

}ret = *(unsigned char *)pchstr1 - *(unsigned char *)pchstr2;

return ret;}//

int mystrlen(const char *pchdest

int ret = 0;

while(*pchdest != 0)

return ret;

}int mystrncmp(const char *pchstr1, const char *pchstr2, size_t count)

if (count == 0)

int ret = 0;

while (count--)

pchstr1++;

pchstr2++;//必須放到上個if後面再++

continue;

}else

}ret = *(unsigned char *)--pchstr1 - *(unsigned char *)--pchstr2;

return ret;//必須--才能返回正確的值,否則會向後移乙個字元才比較}/

void *mymovememery(void *pdest, const void *psrc, size_t nsize)

if (nsize == 0)

//input detected

void *ret = pdest;//remain the old pointer

unsigned char *d = (unsigned char *)pdest;//change to the most small memory unit

unsigned char *s = (unsigned char *)psrc;

if (d < s + nsize)//special memory overlap

}else

}return ret;

//return the old pointer

}char *mystrcat(char *pchdestination, const char *pchsource)

char *ret = pchdestination;

while (*pchdestination != '/0')

while (*pchsource != '/0')

*pchdestination = '/0';

return ret;

}char *mystrncat(char *pchdestination, const char *pchsource, size_t count)

if (count == 0)

char *ret = pchdestination;

while (*pchdestination != '/0')

while (count--)

else

}   

*pchdestination = '/0';

return ret;

}char *mystrchr(const char *pstr, int find)

if (find == 0)

char *ret = (char *)pstr;

while (*ret != '/0')

ret++;

}return null;

}void *mymemchr(const void *pstr, int find, size_t count)

const unsigned char *p = (const unsigned char *)pstr;//為什麼要轉換成這種型別呢?

while (count--)

else

}return null;

}int mymemcmp(const void *pchstr1, const void *pchstr2, size_t count)

if (count == 0)

int ret = 0;

const unsigned char *p1 = (const unsigned char *)pchstr1;//比較記憶體都要轉換為最小儲存單位

const unsigned char *p2 = (const unsigned char *)pchstr2;

while (count--)

else

}return ret;}/

void *mymemset(void *pchdest, int set, size_t count)

if (count == 0)

unsigned char *p = (unsigned char *)pchdest;

unsigned char s = (unsigned char)set;

while (count--)

return pchdest;

}

自己實現的string的庫函式

為了更好地理解string的各個庫函式,現將幾個常用的庫函式用自己的方式實現如下 includeusing namespacestd include 求字串的長度 注 0 的長度不計算在內 int my strlen char str int len 0 while str 0 0 不計算在內 re...

07庫函式編寫

1 字串查詢 給定乙個字串a,要求在a中查詢乙個子串b。如a abcdf 要你在a中查詢子串b cd 初步 如下 int strstr char string,char substring if j lensub return i 1 return 1 針對這個strstr的函式,我覺得有點小問題。...

07庫函式編寫

1 字串查詢 給定乙個字串a,要求在a中查詢乙個子串b。如a abcdf 要你在a中查詢子串b cd 初步 如下 int strstr char string,char substring if j lensub return i 1 return 1 針對這個strstr的函式,我覺得有點小問題。...