C语言库函数源代码(共67页).docx

上传人:飞****2 文档编号:8526243 上传时间:2022-03-19 格式:DOCX 页数:67 大小:27.66KB
返回 下载 相关 举报
C语言库函数源代码(共67页).docx_第1页
第1页 / 共67页
C语言库函数源代码(共67页).docx_第2页
第2页 / 共67页
点击查看更多>>
资源描述

《C语言库函数源代码(共67页).docx》由会员分享,可在线阅读,更多相关《C语言库函数源代码(共67页).docx(67页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上C语言库函数源代码 strstr ( )/* - C语言库函数源代码 - */ /*得到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)break;i+;

2、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 "%s"!n",p);str1 = "abc"str2 =

3、 "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("pause");return 0; strpbrk ( )/* - C语言库函数源代码 - */ /*得到s1中第一个且是s2中字符的位置指针。*/#include <stdlib.h>cha

4、r * 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 = '0')s1 = NULL;return (char *) s1;int main()char *str1 = "ammana_babi"char *str2 = "babi"char *p;if( (

5、p = my_strpbrk(str1,str2) = NULL)printf("No same character!n");elseprintf("%cn",*p);str1 = "abc"str2 = "def"if( (p = my_strpbrk(str1,str2) = NULL)printf("No same character!n");elseprintf("%cn",*p);system("pause");return 0; strcspn

6、 ( )/* - C语言库函数源代码 - */ /*得到s1中第一个且是s2中字符的字符位置。*/int my_strcspn(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)break;s1+;return s1 - s;int main()char *str1 = "ammana_babi"char *str2 = "babi"int offset;if(of

7、fset = my_strcspn(str1,str2) >= strlen(str1)printf("Can'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");e

8、lseprintf("%cn",*(str1 + offset);system("pause");return 0;strspn ( )/* - C语言库函数源代码 - */ /*得到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+;retu

9、rn 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 = "abc"str2 = "abc"if(o

10、ffset = my_strspn(str1,str2) >= strlen(str1)printf("Can't find the different character!n");elseprintf("%cn",*(str1 + offset);system("pause");return 0;strrev ( )/* - C语言库函数源代码 - */ /*Reverses the order of characters in the string.The terminating null character rem

11、ains 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+ = *right;*right- = ch;return(str);/*而我自己写的代码就略微显的有些啰里啰嗦,不简洁,更不是很太爽快。这个问题是值得好好想一下的了。下面就是我的垃圾代码*/char

12、 * 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()char str ="ammana_babi"puts(my_strrev(str);puts(my_StrReverse(str);system("pause")

13、;return 0; strnset ( )/* - C语言库函数源代码 - */ /*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 place of n.把字符串的前count个字符设置为字符val。*/char * my_strnset(char * str,int val,int count)char *p = str;while (count- &&a

14、mp; *p)*p+ = (char)val;return(p);int main()char str ="ammana_babi"my_strnset(str,'*',strlen(str)-4);puts(str);system("pause");return 0;strset ( )/* - C语言库函数源代码 - */ /*Sets all of characters in string (except the terminating '/0'character) equal to val.把字符串的所有字符都设置

15、为字符val。*/char * my_strset(char *str,int val)char *p = str;while (*str)*str+ = (char)val;return(p);int main()char str ="ammana_babi"my_strs et(str,'z');puts(str);system("pause");return 0;strupr ( )/* - C语言库函数源代码 - */ /*Force string to lower case。将字符串转换为大写。只改变字符串中出现的小写字母,不改

16、变其他字符。*/char * my_strupr(char *str)char *p = str;while (*p != '0')if(*p >= 'a' && *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

17、_strupr(str1);puts(my_strupr(str2);puts(my_strupr(str3);puts(my_strupr(str4);system("pause");return 0;strlwr ( )/* - C语言库函数源代码 - */ /*Force string to lower case。将字符串转换为小写。只改变字符串中出现的大写字母,不改变其他字符。*/char * my_strlwr(char *str)char *p = str;while (*p != '0')if(*p >= 'A' &

18、;& *p <= '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_strlwr(str4);system("pause&q

19、uot;);return 0;strdup ( )/* - C语言库函数源代码 - */ /*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()释放。需要注意的是,在调用完这个函数后,一定要记得释放内存空间吆。*/#include <stdlib.h>int my_strlen (

20、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 = malloc(my_strlen(str) + 1)return(my_strcpy(p,str

21、);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语言库函数源代码 - */ /*Finds the last occurrence of ch in string. The terminating null character is used as part of the search.查找在字符

22、串中最后一次出现字符ch的位置。如果str中存在字符ch,返回出现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 (*str = (char)ch) re turn( (char *)str );return(NULL);int main()char *str = "ammana_ba

23、bi"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 !n",*p);ch = 'b'p = (char *)my_strrchr(str,ch);if(p = NULL)printf("Can't find the character %c !n

24、",ch);elseprintf("Find the character %c !n",*p);system("pause");return 0;strchr ( )/* - C语言库函数源代码 - */ #include <stdlib.h>/*Searches a string for a given character, which may be the null character '0'. 查找字符串string中首次出现字符ch的位置。如果string中存在字符ch,返回首次出现ch的位置的指针;否则返回

25、NULL。*/char * my_strchr(const char *str, int ch)while (*str && *str != (char)ch)str+;if (*str = (char)ch)return(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 cha

26、racter %c !n",ch);elseprintf("Find the character %c !n",*p);ch = '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语言库函数源

27、代码 - */ /*Sets the first "count" bytes of the memory starting at "dst" to the character value "val".把dst所指内存区域的前count个字节设置为val。返回指向dst的指针。在实际应用中,我们有时候会用malloc函数来申请一些内存空间,这个内存空间有时候需要初始化,常用memset来进行初始化。如:int *p;p = (int *)malloc( 0x400 * sizeof(int);memset(p,0,0x400);*/vo

28、id * my_memset(void *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语言库函数源代码 - */ /*memicmp perfo

29、rm a case-insensitive memory comparision.For differences,upper case letters are mapped to lower case.Thus, "abc_" < "ABCD" since "_" < "d".(与memcmp区别就是在比较的时候不区分大小写)比较内存区域buffer1和buffer2的前count个字节。当buffer1 < buffer2时,返回值 < 0;当buffer1 = buffer2时,返回值

30、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 count) int f = 0;int l = 0;while (count-)if ( (*(unsigned char *)buffer1 = *(unsigned char *)bu

31、ffer2) |(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 * str1,char *str2,int t)if(t > 0)printf("n%s Upper Than %sn",str1,str2);else if(t &l

32、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"char *str3 = "AMMANA"char *str4 = "bab_"Print(str1,str2,my_memicmp(str1,str2,4);Print(str3,str1,my_memicmp(st

33、r3,str1,4);Print(str4,str2,my_memicmp(str4,str2,4);system("pause");return 0;memcmp ( ) /* - C语言库函数源代码 - */ /*Compares count bytes of memory starting at buffer1 and buffer2 and find if equal or which one is first in lexical order.比较内存区域buffer1和buffer2的前count个字节。当buffer1 < buffer2时,返回值 &l

34、t; 0;当buffer1 = buffer2时,返回值 0;当buffer1 > buffer2时,返回值 > 0。*/int my_memcmp(const void *buffer1,const void *buffer2,int count)if (!count)return(0);while ( -count && *(char *)buffer1 = *(char *)buffer2) buffer1 = (char *)buffer1 + 1;buffer2 = (char *)buffer2 + 1;return( *(unsigned char *

35、)buffer1) - *(unsigned char *)buffer2) );void Print(char * str1,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

36、 *str2 = "babi"Print(str1,str2,my_memcmp(str1,str2,3);Print(str2,str1,my_memcmp(str2,str1,3);Print(str2,str2,my_memcmp(str2,str2,3);system("pause");return 0;memchr ( )/* - C语言库函数源代码 - */ #include <stdlib.h>/*Searches at bufferfor the given character, stopping when character

37、is first found or cnt bytes have been searched through.从buffer所指内存区域的前count个字节查找字符ch,当第一次遇到字符ch时停止查找。如果成功,返回指向字符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

38、-;return(count ? (void *)buffer : NULL);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

39、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);system("pause");return 0;memccpy ( ) /* - C语言库函数源代码 - */ #include <stdlib.h>/*Copies bytes from src to dest until co

40、unt bytes have been copied,or up to and including the character c, whichever comes first.如果src前n个字节中存在c,返回指向字符c后的第一个字符的指针;否则返回NULL,src被复制。*/void * my_memccpy(void *dest,const void *src,int c,int count)while ( count && (*(char *)(dest = (char *)dest + 1) - 1) =*(char *)(src = (char *)src + 1)

41、 - 1) != (char)c )count-;return(count ? dest : NULL);/*这个函数的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;src = (char *)src + 1;count-;return(count ? dst :

42、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("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 character. n");elseprintf("nFind the character! n");*p = '0'printf("nThe String wh

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 教育教学

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁