《C语言笔记:一些自实现的字符串函数》

一,int myStrlen(const char *s);

size_t myStrlen(const char *s)
{
    size_t cnt = 0;
    while(*s++ != '\0')
    {
        cnt++;
    }

    return cnt;
}

二,char *myStrcpy(char *dest, const char *src);

char *myStrcpy(char *dest, const char *src)
{
    char *temp = dest;

    if(src == NULL || dest == NULL)
    {
        return NULL;
    }
        
    
    while((*dest++ = *src++) != '\0');

    return temp;
}

三,char *myStrncpy(char *dest, const char *src, size_t n)

char *myStrncpy(char *dest, const char *src, size_t n)
{
    char *temp = dest;
    if(!n)
    {
        return temp;
    }
   if(src == NULL || dest == NULL)
    {
        return NULL;
    }
  while( (n--) && (*dest++ = *src++) != '\0'); *dest = '\0';
  
   return temp;
}

 四,void *myMemset(void *s, int c, size_t n);

void *myMemset(void *s, int c, size_t n)
{
    char *tmp = (char *)s;
    while(n)
    {
        tmp[n-- - 1] = c;
    }

    return s;
}

五,char *myStrcat(char *dest, const char *src);

char *myStrcat(char *dest, const char *src)
{
    char *tmp = dest;
    if(src == NULL || dest == NULL)
    {
        return NULL;
    }

    while(*dest++ != '\0');

    dest--;

    while((*dest++ = *src++) != '\0');

    return tmp;

}

6,char *strncat(char *dest, const char *src, size_t n);

char *myStrncat(char *dest, const char *src, size_t n)
{

    char *tmp = dest;
    if(src == NULL || dest == NULL)
    {
        return NULL;
    }

    while(*dest++ != '\0');

    dest--;

    while((n--) && (*dest++ = *src++) != '\0');

    *dest = '\0';
}

 

 7,int strcmp(const char *s1, const char *s2);

int myStrcmp(const char *s1, const char *s2)
{
    int s1Len = myStrlen(s1); 
    int s2Len = myStrlen(s2);
    int min = (s1Len>s2Len?s2Len:s1Len);
    int i = 0;

    for(i;i<min;i++)
    {
        if(*s1 != *s2)
        {
            return *s1-*s2;
        }

        s1++;
        s2++;
    }

    return 0;

}

 

8,int strncmp(const char *s1, const char *s2, size_t n);

int myStrncmp(const char *s1, const char *s2, size_t n)
{

    int s1Len = myStrlen(s1); 
    int s2Len = myStrlen(s2);
    int min = (s1Len>s2Len?s2Len:s1Len);
    int i = 0;

    min = (min>n?n:min);

    for(i;i<min;i++)
    {
        if(*s1 != *s2)
        {
            return *s1-*s2;
        }

        s1++;
        s2++;
    }

    return 0;


}

 9,char *myStrchr(const char *s, int c);

char *myStrchr(const char *s, int c)
{
    if(NULL == s || (char)0 == c)
    {
        return NULL;
    }

    while((*s++ != c) && (*s != '\0'));

    if(*s == '\0')
    {
        return NULL;
    }
    else
    {
        return --s;
    }
    

}

 

10,char *myStrrchr(const char *s, int c);

char *myStrrchr(const char *s, int c)
{
    char head;
    head = *s;
    
    //printf(" debug  %c  \n",head);
    
    while(*s++ != '\0');

    while((*s-- != c) && (*s != head));
    
    if(*s == head && *s != c)
    {
        return NULL;
    }
    else
    {
        return ++s;
    }

}

 11,char *myStrstr(const char *haystack, const char *needle);

char *myStrstr(const char *haystack, const char *needle)
{
    int hayLen = myStrlen(haystack);
    int needLen = myStrlen(needle);
    int equlNum = 0;
    char *fristPos = NULL;
    char *pos = needle;

    if(needLen>hayLen)
    {
        return NULL;
    }

    while(*haystack != '\0')
    {
        if(*pos == *haystack)
        {
            if(0==equlNum)
            {
                fristPos = haystack;
            }
            else if(equlNum == needLen-1)
            {
                return fristPos;
            }
            equlNum++;
            pos++;
        }
        else
        {
            equlNum = 0;
            pos = needle;
        }
        
        haystack++;
    }    

    return NULL;

}

 

posted @ 2020-11-17 22:39  xuxianshen  阅读(93)  评论(0编辑  收藏  举报