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 (s2[i] == 0)
{
return (char *) s1;
}
if (s2[i] != s1[i])
{
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("Can't find the string \"%s\"!\n",str2);
else
printf("Find the string \"%s\"!\n",p);
str1 = "abc";
str2 = "def";字符串函数源码
if( (p = my_strstr(str1,str2)) == NULL)
printf("Can't find the string \"%s\"!\n",str2);
else
printf("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");
else
printf("%c\n",*p);
str1 = "abc";
str2 = "def";
if( (p = my_strpbrk(str1,str2)) == NULL)
printf("No same character!\n");
else
printf("%c\n",*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("Can't find the same character!\n");
else
printf("%c\n",*(str1 + offset));
str1 = "abc";
str2 = "def";
if((offset = my_strcspn(str1,str2)) >= strlen(str1))
printf("Can't find the same character!\n");
else
printf("%c\n",*(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("Can't find the different character!\n");
else
printf("%c\n",*(str1 + offset));
str1 = "abc";
str2 = "abc";
if((offset = my_strspn(str1,str2)) >= strlen(str1))
printf("Can't find the different character!\n");
else
printf("%c\n",*(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 '/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";
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("Can't find the character %c !\n",ch);
else
printf("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",ch);
else
printf("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("Can't find the character %c !\n",ch);
else
printf("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);
else
printf("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;
}
else
break;
}
return ( f - l );
}
void Print(char * str1,char *str2,int t)
{
if(t > 0)
printf("\n%s Upper Than %s\n",str1,str2);
else if(t < 0)
printf("\n%s Lower Than %s\n",str1,str2);
else
printf("\n%s Equal %s\n",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 %s\n",str1,str2);
else if(t < 0)
printf("\n%s Lower Than %s\n",str1,str2);
else
printf("\n%s Equal %s\n",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("Can't find the character %c !\n",ch);
else
printf("Find the character %c !\n",*p);
ch = 'b';
p = (char *)my_memchr(str,ch,strlen(str)+1);
if(p == NULL)
printf("Can't find the character %c !\n",ch);
else
printf("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 a[12];
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");
else
{
printf("\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");
else
{
printf("\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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。