记录一下C语言的库函数源码

1、strlen函数

/* 指针相减 */
int strlen1(char *s)
{
    char *p = s;
    while (*p != '\0')
        p++;
    return p - s;
}
/* 中间变量记录长度 */
int strlen2(const char *s)
{
    int i = 0;
    while (*s++ != '\0')
        i++;
    return i;
}
/* 递归 */
int strlen3(const char*s)
{
    assert(NULL != s);
    return ('\0'!= *s)?(1+strlen3(s+1)):0;
}

2、strcat函数

char *strcat1(char *dst,char const *src)
{
    if (dst == NULL || src == NULL)
    {
        return NULL;
    }
    /* 保存开始地址 */
    char *tmp=dst;
    /* 指向末尾地址 */
    while(*dst	!= '\0'){
        dst++;
    }
    /* 赋值 */
    while(*src != '\0'){
        *dst++ = *src++;
    }
    *dst = '\0';
    return tmp;
}

3、strncat函数

char *strncat1(char *dst,char const *src,size_t n)
{
    if (dst == NULL || src == NULL)
    {
        return NULL;
    }
    /* 保存开始地址 */
    char *tmp=dst;
    /* 指向末尾地址 */
    while(*dst	!= '\0'){
        dst++;
    }
    /* 赋值 */
    while(*src != '\0' && n-- > 0){
        *dst++ = *src++;
    }
    *dst = '\0';
    return tmp;
}

4、strcpy函数

char *strcpy1(char *dst, const char *src)
{
    if (dst == NULL || src == NULL){
       return NULL;
    }
    /* 保存开始地址 */
    char *tmp=dst;
    /* 开始拷贝 */
    while ( *src != '\0' ){
        *dst++ = *src++;
    }

    *dst = '\0';
    return tmp;
}

5、strncpy函数

char *strncpy1(char *dst, const char *src, size_t n)
{
    if (dst == NULL || src == NULL){
       return NULL;
    }
    /* 保存开始地址 */
    char *tmp=dst;
    /* 开始拷贝 */
    while ( *src != '\0' && n-- > 0){
        *dst++ = *src++;
    }

    *dst = '\0';
    return tmp;
}

6、strcmp函数

int strcmp(char *dst,char *src)
{
    if (dst == NULL || src == NULL){
       return NULL;
    }
    while(*dst++ == *src++){
        if(*dst == '\0')
            return 0;
    }
    return *dst - *src;
}

7、strncmp函数


int strncmp1(char *dst,char *src,size_t n)
{
    if (dst == NULL || src == NULL){
       return NULL;
    }
    while(*dst == *src && n-- > 0){
        if(*dst == '\0')
            return 0;
        dst++;
        src++;
    }
    printf("1  %d,%d %d\n",n,*dst,*src);
    return *dst - *src;
}

8、strchr函数

char *strchr(char *s, int c)
{
    while(*s != '\0' && *s != c){
        ++s;
    }
    return *s==c ? s : NULL;
}

9、strrchr函数

char *strrchr(char *s, int c)
{
    char *p = s;
    while (*s)
        s++;
    while(s-- != p && *s != c){

    }
    return *s==c ? s : NULL;
}

10、strstr函数

char *strstr(const char *haystack, const char *needle)
{
    char *cp = (char*)haystack;
    char *s1, *s2;
    if (!*needle)
        return((char *)haystack);
    while (*cp){
        s1 = cp;
        s2 = (char *)needle;
        while (*s1 && *s2 && !(*s1 - *s2)){
            s1++;
            s2++;
        }
        if (!*s2)
            return(cp);
        cp++;
    }
    return(NULL);
}

11、atoi函数

int atoi1(const char *nptr)
{
    int sign = 0;/*record the sign*/
    int n = 0;/*record the integer*/
    while(isspace(*nptr))
        nptr++; /*skip white space*/
    sign = ((*nptr == '-') ? -1 : 1);
    if((*nptr == '+') ||(*nptr == '-'))
        nptr++; /*skip sign*/
    while(*nptr != '\0' && isdigit(*nptr)){
        n = n*10 + (*nptr -0x30);
        nptr++;
    }
    return n*sign;  /*return the integer value*/
}

12、itoa函数

void reverse(char str[], int length) 
{ 
    int start = 0; 
    int end = length -1; 
    while (start < end) 
    { 
        swap(*(str+start), *(str+end)); 
        start++; 
        end--; 
    } 
}
char* itoa1(int num, char* str, int base) 
{ 
    int i = 0; 
    bool isNegative = false; 
  
    /* Handle 0 explicitely, otherwise empty string is printed for 0 */
    if (num == 0) 
    { 
        str[i++] = '0'; 
        str[i] = '\0'; 
        return str; 
    } 
  
    // In standard itoa(), negative numbers are handled only with  
    // base 10. Otherwise numbers are considered unsigned. 
    if (num < 0 && base == 10) 
    { 
        isNegative = true; 
        num = -num; 
    } 
  
    // Process individual digits 
    while (num != 0) 
    { 
        int rem = num % base; 
        str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; 
        num = num/base; 
    } 
  
    // If number is negative, append '-' 
    if (isNegative) 
        str[i++] = '-'; 
  
    str[i] = '\0'; // Append string terminator 
  
    // Reverse the string 
    reverse(str, i); 
  
    return str; 
}

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值