C语言 百分网手机站

C语言常用库函数实现

时间:2020-10-04 09:27:53 C语言 我要投稿

C语言常用库函数实现

  对于C语言来说远着许多的函数需要记住,因此是十分考验技巧的语言,但是也是学习编程的入门知识,下面是小编为大家搜集整理出来的有关于C常用库函数实现,希望可以帮助到大家!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
C常用库函数实现
// ---------- strlen -------------
int strlen(char *t){
    int length = 0;
    if(t == NULL)
        return -1;
 
    while (*t != '') {
        t++;
        length++;
    }
    return length;
}
 
size_t strlen(const char *s)
{
    const char *sc;
 
    for (sc = s; *sc != ''; ++sc);
 
    return sc - s;
}
 
// ---------- trim -------------
 
void ltrim ( char *s )
{
    char *p;
    p = s;
    while ( *p == ' ' || *p == ' ' ) {p++;}
    strcpy ( s,p );
}
 
void rtrim ( char *s )
{
    int i;
 
    i = strlen ( s )-1;
    while ( ( s[i] == ' ' || s[i] == ' ' ) && i >= 0 ) {i--;};
    s[i+1] = '';
}
 
void trim ( char *s )
{
    ltrim ( s );
    rtrim ( s );
}
 
// ---------- strcpy -------------
 
char *strcpy(char *dest, const char *src)
{
    char *tmp = dest;
 
    while ((*dest++ = *src++) != '');
 
    return tmp;
}
 
// ---------- strcat -------------
 
char *strcat(char *dest, const char *src)
{
    char *tmp = dest;
 
    while (*dest)
        dest++;
    while ((*dest++ = *src++) != '');
 
    return tmp;
}
 
// ---------- strstr -------------
 
char *strstr(const char *s1, const char *s2)
{
    int l1, l2;
 
    l2 = strlen(s2);
    if (!l2)
        return (char *)s1;
    l1 = strlen(s1);
    while (l1 >= l2) {
        l1--;
        if (!memcmp(s1, s2, l2))
            return (char *)s1;
        s1++;
    }
 
    return NULL;
}
 
 
// ---------- memcmp -------------
 
int memcmp(char *cs, char *ct, size_t count)
{
    char *su1, *su2;
    int res = 0;
 
    for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
        if ((res = *su1 - *su2) != 0)
            break;
    return res;
}
 
// ---------- strcmp -------------
 
int strcmp(const char *cs, const char *ct)
{
    unsigned char c1, c2;
 
    while (1) {
        c1 = *cs++;
        c2 = *ct++;
        if (c1 != c2)
            return c1 < c2 ? -1 : 1;
        if (!c1)
            break;
    }
 
    return 0;
}


【C语言常用库函数实现】相关文章:

C语言的HashTable简单实现11-21

如何实现C语言画图教程11-19

冒泡排序(C语言实现)12-07

希尔排序(C语言实现)12-05

C语言字符串操作函数和常用的实现12-20

C语言中实现KMP算法实例11-19

PID算法的C语言实现12-16

AVL树的c语言实现11-17

c语言入门:用qt实现简单IDE12-14