首页 > 代码库 > WY c语言进阶

WY c语言进阶

1)字符串函数strlen的实现

#include <stdio.h>
#include <string.h>

size_t mylen(const char* s)
{
    int cnt = 0;
    int idx = 0;
    while (s[idx] != \0 ){
        idx++;
        cnt++;
    }
    return cnt;
}

int main(int argc, char const *argv[])
{
    char line[] = "HELLO";
    printf("strlen=%lu\n", mylen(line));
    
    return 0;
    
}


2)strcpy 函数实现

char* mycpy(char* dst, const char* src)
{
    char* ret = dst;
    while(*src != \0){
        *dst++ = *src++;
    }
    *dst =\0‘;
    
    return rest;
}

 

WY c语言进阶