文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>string.h源码

string.h源码

时间:2010-08-25  来源:zhm_sunboy

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_strset(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) 
         return( (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;
}
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载