手写C语言字符库

鉴于以前碰到过很多这样的题目,甚至上次月考核也考了,马上就要考试了,就再重新写一遍,加深印象,但是肯定和库函数有区别,丢失许多细节

1.strlen函数(求字符串长度)

int strlen(char *str)   
{
    int cnt = 0;
    while(*str != '\0') {
        cnt++;
        str++;
    }
    return cnt;
}

2.strcat函数(字符串拼接)

char *strcat(char *s1, char *s2)
{
    while (*s1!='\0') s1++;
    while (*s2!='\0') {
        *s1 = *s2;
        s1++;
        s2++;
    }
    *s1 = '\0';
    
    return s1;
}

3.strcpy函数(字符串复制)

void strcpy(char* s1, char* s2)
{
    while(*s2!='\0') {
        *s1=*s2;
        s1++;
        s2++;
    }
    *s1='\0';
}
posted @ 2019-03-08 17:14  sparkyen  阅读(325)  评论(0编辑  收藏  举报