模拟实现:strlen,strcat,strchr,strcpy,strcat...

本文模拟实现了C语言中常用的字符串处理函数,包括strlen、strcat、strncat、strchr、strrchr、strcpy和strncpy。这些函数在实际编程中非常实用,用于字符串长度计算、拼接、查找特定字符等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟实现的字符串处理函数:
strlen,strcat,strncat,strchr,strrchr,strcpy,strncpy

1.strlen,求字符串的长度,不算‘/0’

size_t mystrlen(const char *string)
{
    const char *p = string;
    while (*p++)
    {}
    return(p - string - 1);
}

2.strcat,拼接字符串

char *mystrcat(char *dest, const char *source)//两个字符串拼接
{
    char *address = dest;
    assert((*dest != NULL) && (*source != NULL));
    while (*dest++)
    {}
    while (*(dest++) = *(source++))
    {}
    return address;
}

3.strncat,拼接字符串,把第二串字符串前count字符拼接到第一个字符串后面

char *mystrncat(char *dest, const char* source,size_t count)//把2字符串前count个接到1字符串后面
{
    char *address = dest;
    assert((*dest != NULL) && (*source != NULL));
    while (*dest != '\0')
    {
        dest++;
    }
    while (count && (*dest++ = *source++))
    {
        count--;
    }
    *dest = '\0';
    return address;
}

4.strchr,找出第一次出现字符C的位置

const char * mystrchr(const char * string,char C)//第一次出现字符的位置,如果没有返回NULL
{
    assert(*string != NULL);
    while (*string)
    {
        if (*string == C)
            return string;
        string++;
    }
    return NULL;
}

5.strrchr,返回字符C最后出现在字符串的位置

const char * mystrrchr(const char * string, char C) //最后一个字符出现的位置,如果没有返回NULL
{
    const char* tmp = NULL;
    assert(*string != NULL);
    while (*string)
    {
        if (*string == C)
            tmp = string;
        string++;
    }
    return tmp;
}

6.strcpy,字符串拷贝

char * mystrcpy(char *dest, const char *source)
{
    char *address = dest;
    assert((dest != NULL)&&(source != NULL));
    while (*dest++ = *source)
    {}
    return address;
}

7.strncpy,字符串拷贝,拷贝源字符串count个字符到目的字符串

char *mystrncpy(char *dest, const char *source, size_t count)
{
    assert((dest != NULL) && (source != NULL));
    char *address = dest;
    while (count--)
    {
        *dest++ = *source++;
    }
    *dest = '\0';
    return address;
}
PS C:\Users\34171\Desktop\code\WJ> g++ -o WJ Recipe.c main.c main.c: In function 'void read_string(char*, int)': main.c:15:21: error: 'strchr' was not declared in this scope 15 | char *newline = strchr(buffer, '\n'); | ^~~~~~ main.c:4:1: note: 'strchr' is defined in header '<cstring>'; did you forget to '#include <cstring>'? 3 | #include <locale.h> +++ |+#include <cstring> 4 | main.c: In function 'int main()': main.c:102:21: error: 'strlen' was not declared in this scope 102 | if (strlen(updated.name) == 0) { | ^~~~~~ main.c:102:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:103:21: error: 'strcpy' was not declared in this scope 103 | strcpy(updated.name, existing->name); | ^~~~~~ main.c:103:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:108:21: error: 'strlen' was not declared in this scope 108 | if (strlen(updated.category) == 0) { | ^~~~~~ main.c:108:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:109:21: error: 'strcpy' was not declared in this scope 109 | strcpy(updated.category, existing->category); | ^~~~~~ main.c:109:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:114:21: error: 'strlen' was not declared in this scope 114 | if (strlen(updated.ingredients) == 0) { | ^~~~~~ main.c:114:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:115:21: error: 'strcpy' was not declared in this scope 115 | strcpy(updated.ingredients, existing->ingredients); | ^~~~~~ main.c:115:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:121:21: error: 'strlen' was not declared in this scope 121 | if (strlen(input) > 0) { | ^~~~~~ main.c:121:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:122:41: error: 'atoi' was not declared in this scope 122 | updated.prep_time = atoi(input); | ^~~~ main.c:127:21: error: 'strlen' was not declared in this scope 127 | if (strlen(input) > 0) { | ^~~~~~ main.c:127:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:128:41: error: 'atoi' was not declared in this scope 128 | updated.cook_time = atoi(input); | ^~~~ main.c:133:21: error: 'strlen' was not declared in this scope 133 | if (strlen(updated.steps) == 0) { | ^~~~~~ main.c:133:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:134:21: error: 'strcpy' was not declared in this scope 134 | strcpy(updated.steps, existing->steps); | ^~~~~~ main.c:134:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? PS C:\Users\34171\Desktop\code\WJ>
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值