仿写C语言string.h头文件检验字符串函数

时间:2021-09-07 07:04:12

c语言string.h头文件字符串检验函数仿写

所需头文件:stdio.h ,stdlib.h, string.h

strlen字符串求长度

?
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
int strlen_my(const char* src_str)
{
    if (NULL == src_str)
    {
        return -1;
    }
    const char* p = src_str;
    while (*p != '\0')
    {  
        p++;
    }
    return p - &(src_str[0]);
}
//递归
int strlen_my2(const char* src_str)
{
    if (src_str == NULL)
        return -1;
    if (*src_str)
    {
        return strlen_my2(src_str + 1) + 1;
    }
    else
    {
        return 0;
    }
}
 int main()
{
    const char* p = "hello";
    printf("%d \n",strlen_my(p));
    return 0;
}

strcmp / strncmp字符串比较

?
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
int strcmp_my(const char* str_a, const char* str_b)
{
    while ((*str_a != '\0' || *str_b != '\0') && *str_a == *str_b )
    {
        str_a++;
        str_b++;
    }
    return *str_a - *str_b > 0 ? 1 : (*str_a - * str_b < 0 ? -1 : 0);
}
int strcmp_my2(const char* str_a, const char* str_b)
{
    int sub = 0;
    while ((sub = *str_a - *str_b) == 0 && *str_a++ && *str_b++);   //先赋值,指针再++
    return sub > 0 ? 1 : (sub < 0 ? -1 : 0);
}
 int strncmp_my(const char* str_a, const char* str_b, size_t front_len)
{
    while (--front_len && *str_a != '\0' && *str_b != '\0'
        && *str_a == *str_b)
    {
        str_a++;
        str_b++;
    }
    return *str_a - *str_b > 0 ? 1 : (*str_a - *str_b < 0 ? -1 : 0);
}
 int main()
{
    const char* p = "hella";
    const char* q = "hell";
    printf("%d \n", strncmp_my(p, q, 4));
    return 0;
}

strchr / strrchr 字符串中查找字符ch

第一个出现的字符ch

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//strchr 返回字符串中第一个出现的字符ch
char* strchr_my(const char* src_str, char ch)
{
    if (src_str == NULL)
    {
        return NULL;
    }
    while (*src_str != '\0' && *src_str != ch)
    {
        src_str++;
    }
    return *src_str == '\0' ? NULL : src_str;
}
 int main()
{
    const char* p = "hello";
    p = strchr_my(p, 'e');
    if (p == NULL)
        return 0;
    printf("%c\n", *p);
}

最后一个出现的字符ch

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//strrchr查找字符串中最后一个出现的字母ch
char* strrchr_my(const char* src_str, char ch)
{
    if (NULL == src_str) return NULL;
    const char* p = src_str + strlen(src_str) - 1;
    while ((p - src_str) >= 0 && *p != ch)
    {
        p--;
    }
    if (p - src_str == -1)
    {
        return NULL;
    }
    return p;
}
 int main()
{
    const char* p = "hello";
    //p = strchr_my(p, 'q');
    p = strrchr_my(p, 'l');
    if (p == NULL)
        return 0;
    printf("%c\n", *p);
}

strstr 字符串匹配:src_str中查找des_str,返回匹配的起始位置,没有为NULL(BF算法)

?
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
char* strstr_my(const char* src_str, const char* des_str)
{
    if (NULL == src_str || NULL == des_str)
    {
        return NULL;
    }
    int len_src = strlen(src_str);
    int len_des = strlen(des_str);
    int i = 0;  //src_str
    int j = 0;  //des_str
    while (i < len_src && j < len_des)
    {
        if (src_str[i] == des_str[j])
        {
            i++;
            j++;
        }
        else
        {
            i = i - j + 1;
            j = 0;
        }
    }
    if (j == len_des)
    {
        return src_str + i - j;
    }
    return NULL;
}
 int main()
{
    const char* p = "hello";
    const char* q = "llo";
    const char* rt = strstr_my(p, q);
    if (rt != NULL)
    {
        printf("%s\n", rt);
        return 0;
    }
    return 0;
}

strcpy / strncpy字符串拷贝

?
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
char* strcpy_my(char* des_str, const char* src_str)
{
    if (des_str == NULL || src_str == NULL)
        return NULL;
    char* p = des_str;
    while (*src_str != '\0')
    {
        *p++ = *src_str++;
    }
    *p = '\0';
    return des_str;
}
 char* strncpy_my(char* des_str, const char* src_str, size_t len)
{
    if (des_str == NULL || src_str == NULL || len <= 0)
        return NULL;
    char* p = des_str;
    //源字符串实际长度
    int cur_len = strlen(src_str) <= len ? len : strlen(src_str);
    while (cur_len--)
    {
        *p++ = *src_str++;
    }
    *p = '\0';
    return des_str;
}
 int main()
{
    const char* p = "hello";
    char q[10] = {0};
    if (strncpy_my(q, p, 1) != NULL)
    {
        printf("%s\n", q);
    }
    return 0;
}

strcat / strncat字符串的粘贴

?
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
char* strcat_my(char* des_str, const char* src_str)
{
    if (des_str == NULL || src_str == NULL)
        return NULL;
    int len_des = strlen(des_str);
    char* p = des_str + len_des;
    while (*src_str != '\0')
    {
        *p++ = *src_str++;
    }
    return des_str;
}
 char* strncat_my(char* des_str, const char* src_str, size_t len)
{
    if (des_str == NULL || src_str == NULL || len < 0)
        return NULL;
    int len_des = strlen(des_str);
    char* p = des_str + len_des;
 
    while (len--)
    {
        *p++ = *src_str++;
    }
    return des_str;
}
 int main()
{
    char p[20] = "hello";
    const char* q = "world";
    printf("p = %s\n", p);
    printf("q = %s\n", q);
    if (strncat_my(p, q, 3) != NULL)
    {
        printf("p = %s\n", p);
    }
    printf("q = %s\n", q);
    return 0;
}

strdup 字符串申请堆区空间存放字符串的副本

?
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
char* strdup_my(const char* src_str)
{
    if (NULL == src_str)
        return NULL;
    char* newspace = (char*)malloc((strlen(src_str) + 1 ) * sizeof(char));
    if (newspace == NULL)
        return NULL;
    int i = 0;
    while (*src_str != '\0')
    {
        newspace[i++] = *src_str++;
    }
    newspace[i] = '\0';
    return newspace;
}
int main()
{
    const char* p = "hello";
    char* q = strdup_my(p);
    if (q != NULL)
    {
        printf("%s\n", q);
        free(q);
        q == NULL;
    }
    return 0;
}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/xiaoxiaoguailou/article/details/121267391