[C/OC的那点事儿]string.h 的库函数以及底层实现.[未完待续]

//
//  main.c
//  string.h
//
//  Created by lichan on 13-11-21.
//  Copyright (c) 2013年 com.lichan. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
int mystrlen(const char *str)
{
    char *cp = (char *)str;
    int length = 0;
    while(*cp)
        cp++,length++;
    return length;

}

char * mystrcpy(char *dest,const char* source)
{
    if (NULL == dest || NULL == source) {
        return NULL;
    }
    
    char *address = dest;
    char *st = (char *)source;
    
    while ((*address++ = *st++))
        ;
    
    *address = '\0';
    
    return dest;

}

char *mystrcat(char *dest,const char *source)
{
    if (NULL == dest || NULL == source) {
        return NULL;
    }
    
    char *address =dest;
    if (!*source)
        return dest;
    char *cs = (char *)source;
    
    while (*dest)
        dest++;
        ;
    while ((*dest++ = *cs++))
        ;
    return address;
}

char *mystrstr(const char *dest,const char* source)
{
    if (NULL == dest || NULL == source) {
        return NULL;
    }
    char *cp = (char *)dest;
    
    if (!*source)
        return cp;
    
    while (*cp) {
        
        char *str1 = cp;
        char *str2 = (char *)source;
        
        while (*str1 && *str2 && !(*str1 - *str2))
            str1++,str2++;
        
        
        if (!*str2)
            return cp;
        
        
        cp++;
    
    }
    
    return NULL;
}

int mystrcmp(const char* dest,const char*source)
{
   //1 >;  0== ;-1<
    
//    if (NULL == dest || NULL == source) {
//        printf("输入的参数地址不正确.\n");
//        return 2;
//    }
    
    char *str1 = (char *)dest;
    char *str2 = (char *)source;
    
    while (*str1 == *str2)
    {
        if (*str1 == '\0') {
            return 0;
        }
        
        str1++,str2++;
    }
    return *str1-*str2;
}



int main(int argc, const char * argv[])
{
    char *str1 = "lichan";
   printf("%d",mystrlen(str1));
    printf("\n---------------\n");
    
    char *dest2 = malloc(sizeof(char)*30);
    char *source2 = "lichan";
    
    printf("strcpy: %s",mystrcpy(dest2, source2));
    printf("\n---------------\n");
    char dest3[30] = "lichan";
    char *source = " nihao!";
    
    printf("strcat: %s",mystrcat(dest3, source));
    
    printf("\n---------------\n");
    char *dest4 ="lichan";
    char *source4 = "sch";
    
    printf("strstr:%s",mystrstr(dest4, source4));
    char *dest5 = "lichan";
    char *source5 = "licshan";
    
    printf("strcmp: %d",mystrcmp(dest5, source5));
    
    
    printf("\n---------------\n");
    printf("\n---------------\n");
    printf("\n---------------\n");
    printf("\n---------------\n");
    printf("\n---------------\n");
    

    // insert code here...
    printf("Hello, World!\n");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值