C语言库函数源代码_第1页
C语言库函数源代码_第2页
C语言库函数源代码_第3页
C语言库函数源代码_第4页
C语言库函数源代码_第5页
已阅读5页,还剩61页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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+;s1+;return (char *) NULL;int main()char *str1 = "ammana_babi"char *str2 = "babi"char *p;if( (p = my_strstr(str1,str2) = NULL)printf("Cant find the string "%s"!n",str2);elseprintf("Find the string "%s"!n",p);str1 = "abc"str2 = "def"if( (p = my_strstr(str1,str2) = NULL)printf("Cant find the string "%s"!n",str2);elseprintf("Find the string "%s"!n",p);system("pause");return 0; strpbrk ( )/* - C语言库函数源代码 - */ /*得到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 = 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"str2 = "def"if( (p = my_strpbrk(str1,str2) = NULL)printf("No same character!n");elseprintf("%cn",*p);system("pause");return 0; strcspn ( )/* - 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(offset = my_strcspn(str1,str2) >= strlen(str1)printf("Cant find the same character!n");elseprintf("%cn",*(str1 + offset);str1 = "abc"str2 = "def"if(offset = my_strcspn(str1,str2) >= strlen(str1)printf("Cant find the same character!n");elseprintf("%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+;return s1 - s;int main()char *str1 = "ammana_babi"char *str2 = "babi"int offset;if(offset = my_strspn(str1,str2) >= strlen(str1)printf("Cant find the different character!n");elseprintf("%cn",*(str1 + offset);str1 = "abc"str2 = "abc"if(offset = my_strspn(str1,str2) >= strlen(str1)printf("Cant 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 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+ = *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()char str ="ammana_babi"puts(my_strrev(str);puts(my_StrReverse(str);system("pause");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- && *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 /0character) 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"my_strs et(str,z);puts(str);system("pause");return 0;strupr ( )/* - C语言库函数源代码 - */ /*Force string to lower case。将字符串转换为大写。只改变字符串中出现的小写字母,不改变其他字符。*/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_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 && *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");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 ( 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);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.查找在字符串中最后一次出现字符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_babi"char * p;char ch;ch = 9;p = (char *)my_strrchr(str,ch);if(p = NULL)printf("Cant 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("Cant find the character %c !n",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的位置的指针;否则返回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("Cant find the character %c !n",ch);elseprintf("Find the character %c !n",*p);ch = b;p = (char *)my_strchr(str,ch);if(p = NULL)printf("Cant find the character %c !n",ch);elseprintf("Find the character %c !n",*p);system("pause");return 0;memset ( )/* - C语言库函数源代码 - */ /*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);*/void * 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 perform 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时,返回值 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 *)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 * 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 *str2 = "babi"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语言库函数源代码 - */ /*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时,返回值 < 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 *)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 *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 characteris 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-;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("Cant find the character %c !n",ch);elseprintf("Find the character %c !n",*p);ch = b;p = (char *)my_memchr(str,ch,strlen(str)+1);if(p = NULL)printf("Cant 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 count 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) - 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 : 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("nCant 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("nCant not find character. 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语言库函数源代码 - */ /*memmove() copies a source memory buffer to a destination memory buffer.This routine recognize overlapping buffers to avoid propogation.For cases where propagation is not a problem, memcpy() can be used.memmove()由src所指定的内存区域赋值count个字符到dst所指定的内存区域。src和dst所指内存区域可以重叠,但复制后src的内容会被更改。函数返回指向dst的指针。*/void * my_memmove(void * dst,const void * src,int count)void * 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;src = (char *)src - 1;return(ret);int main()char a12;puts(char *)my_memmove(a,"ammana_babi",16);system("pause");return 0;memcpy ( ) /* - C语言库函数源代码 - */ /*memcpy() copies a source memory buffer to a destination memory buffer. This routine does NOT recognize overlapping buffers, and thus can lead to propogation.For cases where propagation must be avoided, memmove() must be used.memcpy()由src指定内存区域拷贝count个字符到dst所指定的内存区域。src和dst内存区域不能重叠,函数返回指向dst的指针。*/void * my_memcpy(void *dst,const void *src,int count)void * ret = dst;while (count-) *(char *)dst = *(char *)src;dst = (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语言库函数源代码 - */ /*Compare the two strings for lexical order. Stops the comparison when the following occurs: (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.字符串比较函数,比较字符串src和dst的前count个字符,但是不区分大小写,大写字母会被转换为小写字母来进行比较。如:"abc_" < "ABCD" ,因为 "_" < "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) &&(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 * str1,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"

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论