C語言庫函數(shù)源代碼(共67頁)_第1頁
C語言庫函數(shù)源代碼(共67頁)_第2頁
C語言庫函數(shù)源代碼(共67頁)_第3頁
C語言庫函數(shù)源代碼(共67頁)_第4頁
C語言庫函數(shù)源代碼(共67頁)_第5頁
已閱讀5頁,還剩62頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上C語言庫函數(shù)源代碼 strstr ( )/* - C語言庫函數(shù)源代碼 - */ /*得到s1中第一次包含s2字符串的位置指針。*/#include <stdlib.h>char * my_strstr(const char *s1,const char *s2)if (*s1 = 0)if (*s2)return (char *) NULL;return (char *) s1;while (*s1)size_t i;i = 0;while (1)if (s2i = 0)return (char *) s1;if (s2i != s1i)b

2、reak;i+;s1+;return (char *) NULL;int main()char *str1 = "ammana_babi"char *str2 = "babi"char *p;if( (p = my_strstr(str1,str2) = NULL)printf("Can't find the string "%s"!n",str2);elseprintf("Find the string &q

3、uot;%s"!n",p);str1 = "abc"str2 = "def"if( (p = my_strstr(str1,str2) = NULL)printf("Can't find the string "%s"!n",str2);elseprintf("Find the string "%s"!n",p);system(&

4、amp;quot;pause");return 0; strpbrk ( )/* - C語言庫函數(shù)源代碼 - */ /*得到s1中第一個且是s2中字符的位置指針。*/#include <stdlib.h>char * my_strpbrk(const char *s1 ,const char *s2)const char *c = s2;if (!*s1)return (char *) NULL;while (*s1)for (c = s2; *c; c+)if (*s1 = *c)break;if (*c)break;s1+;if (*c = &

5、amp;#39;0')s1 = NULL;return (char *) s1;int main()char *str1 = "ammana_babi"char *str2 = "babi"char *p;if( (p = my_strpbrk(str1,str2) = NULL)printf("No same character!n");elseprintf("%cn",*p);str1 = "abc&quo

6、t;str2 = "def"if( (p = my_strpbrk(str1,str2) = NULL)printf("No same character!n");elseprintf("%cn",*p);system("pause");return 0; strcspn ( )/* - C語言庫函數(shù)源代碼 - */ /*得到s1中第一個且是s2中字符的字符位置。*/int my_strcspn(const char *s1 ,const char *

7、s2)const char *s = s1;const char *p;while (*s1)for (p = s2; *p; p+)if (*s1 = *p)break;if (*p)break;s1+;return s1 - s;int main()char *str1 = "ammana_babi"char *str2 = "babi"int offset;if(offset = my_strcspn(str1,str2) >= strlen(str1)printf("Can&

8、#39;t find the same character!n");elseprintf("%cn",*(str1 + offset);str1 = "abc"str2 = "def"if(offset = my_strcspn(str1,str2) >= strlen(str1)printf("Can't find the same character!n");elseprintf("

9、%cn",*(str1 + offset);system("pause");return 0;strspn ( )/* - C語言庫函數(shù)源代碼 - */ /*得到s1中第一 個且不是s2中任意字符的字符位置。*/int my_strspn(const char *s1 ,const char *s2)const char *s = s1;const char *p;while (*s1)for (p = s2; *p; p+)if (*s1 = *p)break;if (*p = '0')break;s1+;

10、return s1 - s;int main()char *str1 = "ammana_babi"char *str2 = "babi"int offset;if(offset = my_strspn(str1,str2) >= strlen(str1)printf("Can't find the different character!n");elseprintf("%cn",*(str1 + offset);str1

11、 = "abc"str2 = "abc"if(offset = my_strspn(str1,str2) >= strlen(str1)printf("Can't find the different character!n");elseprintf("%cn",*(str1 + offset);system("pause");return 0;strrev ( )/* - C語言庫函數(shù)

12、源代碼 - */ /*Reverses the order of characters in the string.The terminating null character remains in place.把字符串的所有字符的順序顛倒過來(不包括空字符NULL)。返回指向顛倒順序后的字符串指針。*/char * my_strrev(char *str)char *right = str;char *left = str;char ch;while (*right) right+;right-;while (left < right)ch = *left;*left+ = *

13、right;*right- = ch;return(str);/*而我自己寫的代碼就略微顯的有些啰里啰嗦,不簡潔,更不是很太爽快。這個問題是值得好好想一下的了。下面就是我的垃圾代碼*/char * my_StrReverse(char * ch)char tempch,* tch;int Len,i;tch = ch;Len = strlen(ch);for(i=0;i<Len/2;i+)tempch = *tch;*tch = *(tch + Len - 2*i - 1);*(tch+Len-2*i-1) = tempch;tch+;return ch;int main()ch

14、ar str ="ammana_babi"puts(my_strrev(str);puts(my_StrReverse(str);system("pause");return 0; strnset ( )/* - C語言庫函數(shù)源代碼 - */ /*Sets the first count characters of string the character value.If the length of string is less than count, the length of string is used in pl

15、ace of n.把字符串的前count個字符設置為字符val。*/char * my_strnset(char * str,int val,int count)char *p = str;while (count- && *p)*p+ = (char)val;return(p);int main()char str ="ammana_babi"my_strnset(str,'*',strlen(str)-4);puts(str);system("pause"

16、);return 0;strset ( )/* - C語言庫函數(shù)源代碼 - */ /*Sets all of characters in string (except the terminating '/0'character) equal to val.把字符串的所有字符都設置為字符val。*/char * my_strset(char *str,int val)char *p = str;while (*str)*str+ = (char)val;return(p);int main()char str ="ammana_babi&

17、quot;my_strs et(str,'z');puts(str);system("pause");return 0;strupr ( )/* - C語言庫函數(shù)源代碼 - */ /*Force string to lower case。將字符串轉(zhuǎn)換為大寫。只改變字符串中出現(xiàn)的小寫字母,不改變其他字符。*/char * my_strupr(char *str)char *p = str;while (*p != '0')if(*p >= 'a' &a

18、mp;amp;& *p <= 'z')*p -= 0x20;p+;return str;int main()int i;char str1= "Ammana"char str2 = "baBi"char str3 = "AMMANA"char str4 = "aMmAn_BabI" puts(my_strupr(str1);puts(my_strupr(str2);puts(my_strup

19、r(str3);puts(my_strupr(str4);system("pause");return 0;strlwr ( )/* - C語言庫函數(shù)源代碼 - */ /*Force string to lower case。將字符串轉(zhuǎn)換為小寫。只改變字符串中出現(xiàn)的大寫字母,不改變其他字符。*/char * my_strlwr(char *str)char *p = str;while (*p != '0')if(*p >= 'A' && *p &a

20、mp;lt;= 'Z')*p = (*p) + 0x20;p+;return str;int main()int i;char str1= "Ammana"char str2 = "baBi"char str3 = "AMMANA"char str4 = "aMmAn_BabI"puts(my_strlwr(str1);puts(my_strlwr(str2);puts(my_strlwr(str3);puts(my_s

21、trlwr(str4);system("pause");return 0;strdup ( )/* - C語言庫函數(shù)源代碼 - */ /*Allocates enough storage via malloc() for a copy of the string, copies the string into the new memory, and returns a pointer to it.復制字符串,返回指向被復制字符串的指針。所需空間由malloc()分配,且可以由free()釋放。需要注意的是,在調(diào)用完這個函數(shù)后,一定要記得釋放內(nèi)存空間吆。*/#

22、include <stdlib.h>int my_strlen ( const char * str )const char *p = str;while( *p+ ) ;return( (int)(p - str - 1) );char * my_strcpy(char * dst, const char * src)char * cp = dst;while( *cp+ = *src+ ) ; return( dst );char * my_strdup(const char *str)char *p;if (!str)return(NULL);if (p =

23、malloc(my_strlen(str) + 1)return(my_strcpy(p,str);return(NULL);int main()char *str = "ammana_babi"char *p;p = my_strdup("ammana_babi");puts(p);free(p);system("pause");return 0;strrchr ( )/* - C語言庫函數(shù)源代碼 - */ /*Finds the last occurrence of ch in st

24、ring. The terminating null character is used as part of the search.查找在字符串中最后一次出現(xiàn)字符ch的位置。如果str中存在字符ch,返回出現(xiàn)ch的位置的指針;否則返回NULL。*/#include <stdlib.h>char * my_strrchr(const char * str,int ch)char *p = (char *)str;while (*str) str+;while (str- != p && *str != (char)ch);if (*s

25、tr = (char)ch) re turn( (char *)str );return(NULL);int main()char *str = "ammana_babi"char * p;char ch;ch = '9'p = (char *)my_strrchr(str,ch);if(p = NULL)printf("Can't find the character %c !n",ch);elseprintf("Find the character %c

26、 !n",*p);ch = 'b'p = (char *)my_strrchr(str,ch);if(p = NULL)printf("Can't find the character %c !n",ch);elseprintf("Find the character %c !n",*p);system("pause");return 0;strchr ( )/* - C語言庫函數(shù)源代碼 - */ #include &am

27、p;lt;stdlib.h>/*Searches a string for a given character, which may be the null character '0'. 查找字符串string中首次出現(xiàn)字符ch的位置。如果string中存在字符ch,返回首次出現(xiàn)ch的位置的指針;否則返回NULL。*/char * my_strchr(const char *str, int ch)while (*str && *str != (char)ch)str+;if (*str = (char)ch)ret

28、urn(char *)str);return(NULL);int main()char *str = "ammana_babi"char * p;char ch;ch = '9'p = (char *)my_strchr(str,ch);if(p = NULL)printf("Can't find the character %c !n",ch);elseprintf("Find the character %c !n",*p);ch =

29、'b'p = (char *)my_strchr(str,ch);if(p = NULL)printf("Can't find the character %c !n",ch);elseprintf("Find the character %c !n",*p);system("pause");return 0;memset ( )/* - C語言庫函數(shù)源代碼 - */ /*Sets the first "count&

30、;quot; bytes of the memory starting at "dst" to the character value "val".把dst所指內(nèi)存區(qū)域的前count個字節(jié)設置為val。返回指向dst的指針。在實際應用中,我們有時候會用malloc函數(shù)來申請一些內(nèi)存空間,這個內(nèi)存空間有時候需要初始化,常用memset來進行初始化。如:int *p;p = (int *)malloc( 0x400 * sizeof(int);memset(p,0,0x400);*/void * my_memset(void

31、*dst,int val,int count)void *p = dst;while (count-) *(char *)dst = (char)val;dst = (char *)dst + 1;return p;int main()char str ="ammana_babi"my_memset(str,'z',strlen(str);puts(str);system("pause");return 0;memicmp ( )/* - C語言庫函數(shù)源代碼 - */ /*memicmp p

32、erform a case-insensitive memory comparision.For differences,upper case letters are mapped to lower case.Thus, "abc_" < "ABCD" since "_" < "d".(與memcmp區(qū)別就是在比較的時候不區(qū)分大小寫)比較內(nèi)存區(qū)域buffer1和buffer2的前count個字節(jié)。當buffer1 &

33、lt; buffer2時,返回值 < 0;當buffer1 = buffer2時,返回值 0;當buffer1 > buffer2時,返回值 > 0。*/int my_tolower(char ch)if(ch >= 'A' && ch <= 'Z')return (ch + 0x20);return ch;int my_memicmp(const void *buffer1,const void *buffer2,int cou

34、nt) int f = 0;int l = 0;while (count-)if ( (*(unsigned char *)buffer1 = *(unsigned char *)buffer2) |(f = my_tolower( *(unsigned char *)buffer1 ) =(l = my_tolower( *(unsigned char *)buffer2 ) )buffer1 = (char *)buffer1 + 1;buffer2 = (char *)buffer2 + 1;elsebreak;return ( f - l );void Print(char * str

35、1,char *str2,int t)if(t > 0)printf("n%s Upper Than %sn",str1,str2);else if(t < 0)printf("n%s Lower Than %sn",str1,str2);elseprintf("n%s Equal %sn",str1,str2);int main()char *str1= "ammana"char *str2 = "bab

36、i"char *str3 = "AMMANA"char *str4 = "bab_"Print(str1,str2,my_memicmp(str1,str2,4);Print(str3,str1,my_memicmp(str3,str1,4);Print(str4,str2,my_memicmp(str4,str2,4);system("pause");return 0;memcmp ( ) /* - C語言庫函數(shù)源代碼 - */ /*Compares count by

37、tes of memory starting at buffer1 and buffer2 and find if equal or which one is first in lexical order.比較內(nèi)存區(qū)域buffer1和buffer2的前count個字節(jié)。當buffer1 < buffer2時,返回值 < 0;當buffer1 = buffer2時,返回值 0;當buffer1 > buffer2時,返回值 > 0。*/int my_memcmp(const void *buffer1,const void *buffer2

38、,int count)if (!count)return(0);while ( -count && *(char *)buffer1 = *(char *)buffer2) buffer1 = (char *)buffer1 + 1;buffer2 = (char *)buffer2 + 1;return( *(unsigned char *)buffer1) - *(unsigned char *)buffer2) );void Print(char * str1,char *str2,int t)if(t > 0)printf(&quo

39、t;n%s Upper Than %sn",str1,str2);else if(t < 0)printf("n%s Lower Than %sn",str1,str2);elseprintf("n%s Equal %sn",str1,str2);int main()char *str1= "ammana"char *str2 = "babi"Print(str1,str2,my_memcmp(str1,str2,3);

40、Print(str2,str1,my_memcmp(str2,str1,3);Print(str2,str2,my_memcmp(str2,str2,3);system("pause");return 0;memchr ( )/* - C語言庫函數(shù)源代碼 - */ #include <stdlib.h>/*Searches at bufferfor the given character, stopping when characteris first found or cnt bytes have been searched t

41、hrough.從buffer所指內(nèi)存區(qū)域的前count個字節(jié)查找字符ch,當?shù)谝淮斡龅阶址鹀h時停止查找。如果成功,返回指向字符ch的指針;否則返回NULL。*/void * my_memchr(const void * buffer,int ch,int count)while ( count && (*(unsigned char *)buffer != (unsigned char)ch) ) buffer = (unsigned char *)buffer + 1;count-;return(count ? (void *)buffer : NULL);

42、int main()char *str = "ammana_babi"char * p;char ch;ch = '9' ;p = (char *)my_memchr(str,ch,strlen(str)+1);if(p = NULL)printf("Can't find the character %c !n",ch);elseprintf("Find the character %c !n",*p);ch = 'b&am

43、p;#39;p = (char *)my_memchr(str,ch,strlen(str)+1);if(p = NULL)printf("Can't find the character %c !n",ch);elseprintf("Find the character %c !n",*p);system("pause");return 0;memccpy ( ) /* - C語言庫函數(shù)源代碼 - */ #include <stdlib.h>

44、/*Copies bytes from src to dest until count bytes have been copied,or up to and including the character c, whichever comes first.如果src前n個字節(jié)中存在c,返回指向字符c后的第一個字符的指針;否則返回NULL,src被復制。*/void * my_memccpy(void *dest,const void *src,int c,int count)while ( count && (*(char *)(dest = (char *)

45、dest + 1) - 1) =*(char *)(src = (char *)src + 1) - 1) != (char)c )count-;return(count ? dest : NULL);/*這個函數(shù)的while條件判斷寫的比較長,看的眼疼,等價與以下寫法:*/void * my_memccpy01(void *dst,const void *src,int c,int count) while (count) *(char *)dst = *(char *)src;dst = (char *)dst + 1;if(*(char *)src = (char) c)break;sr

46、c = (char *)src + 1;count-;return(count ? dst : NULL);int main()char a12;char * p;char * str ="ammana_babi"char ch;ch = '9'p = (char *)my_memccpy01(a,str,ch,strlen(str)+1);if(p = NULL)printf("nCan't not find character. n");elseprintf(&q

47、uot;nFind the character! n");*p= '0'printf("nThe String which has been copied is:t");puts(a);printf("*");ch = 'b'p = (char *)my_memccpy01(a,str,ch,strlen(str)+1);if(p = NULL)printf("nCan't not find characte

48、r. n");elseprintf("nFind the character! n");*p = '0'printf("nThe String which has been copied is:t");puts(a);system("pause");return 0;memmove ( ) /* - C語言庫函數(shù)源代碼 - */ /*memmove() copies a source memory buffer to a destinat

49、ion memory buffer.This routine recognize overlapping buffers to avoid propogation.For cases where propagation is not a problem, memcpy() can be used.memmove()由src所指定的內(nèi)存區(qū)域賦值count個字符到dst所指定的內(nèi)存區(qū)域。src和dst所指內(nèi)存區(qū)域可以重疊,但復制后src的內(nèi)容會被更改。函數(shù)返回指向dst的指針。*/void * my_memmove(void * dst,const void * src,int count)voi

50、d * ret = dst;if (dst <= src | (char *)dst >= (char *)src + count) while (count-) *(char *)dst = *(cha r *)src;dst = (char *)dst + 1;src = (char *)src + 1;else dst = (char *)dst + count - 1;src = (char *)src + count - 1;while (count-) *(char *)dst = *(char *)src;dst = (char *)dst - 1;s

51、rc = (char *)src - 1;return(ret);int main()char a12;puts(char *)my_memmove(a,"ammana_babi",16);system("pause");return 0;memcpy ( ) /* - C語言庫函數(shù)源代碼 - */ /*memcpy() copies a source memory buffer to a destination memory buffer. This routine does NOT recognize overlapp

52、ing buffers, and thus can lead to propogation.For cases where propagation must be avoided, memmove() must be used.memcpy()由src指定內(nèi)存區(qū)域拷貝count個字符到dst所指定的內(nèi)存區(qū)域。src和dst內(nèi)存區(qū)域不能重疊,函數(shù)返回指向dst的指針。*/void * my_memcpy(void *dst,const void *src,int count)void * ret = dst;while (count-) *(char *)dst = *(char *)src;d

53、st = (char *)dst + 1;src = (char *)src + 1;return(ret);int main()char a12;puts(char *)my_memcpy(a,"ammana_babi",16);system("pause");return 0;strnicmp ( ) /* - C語言庫函數(shù)源代碼 - */ /*Compare the two strings for lexical order. Stops the comparison when the following occur

54、s: (1) strings differ, (2) the end of the strings is reached, or (3) count characters have been compared. For the purposes of the comparison, upper case characters are converted to lower case.字符串比較函數(shù),比較字符串src和dst的前count個字符,但是不區(qū)分大小寫,大寫字母會被轉(zhuǎn)換為小寫字母來進行比較。如:"abc_" < "ABC

55、D" ,因為 "_" < "d"。當源字符串大于目標字符串的時候,返回>0;當源字符串等于目標字符串的時候,返回=0。當源字符串小于目標字符串的時候,返回<0;*/int my_strnicmp(const char *dst,const char *src,int count)int ch1, ch2;do if ( (ch1 = (unsigned char)(*(dst+) >= 'A') &&a

56、mp;amp;(ch1 <= 'Z') )ch1 += 0x20;if ( (ch2 = (unsigned char)(*(src+) >= 'A') &&(ch2 <= 'Z') )ch2 += 0x20; while ( -count && ch1 && (ch1 = ch2) );return (ch1 - ch2);void Print(char * s

57、tr1,char *str2,int t,int n)char *p;p = str1;while(*p && (p-str1) < n) printf("%c",*p),p+;if(t > 0)printf("tUpper Thant");else if(t < 0)printf("tLower Thant");elseprintf("tEqualtt");p = str2;while(*p && (p-str2) < n) printf("%c",*p),p+;printf("n");#define nn 4 int main()char *str1= "ammana"char

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論