自己实现strchr函数与strstr函数

时间:2022-05-27 19:33:24

 

char* my_strchr(char* str, int i)
{
    if (NULL == str)
    {
        return NULL;
    }
    while ('\0' != *str && (char)i != *str)
    {
        ++str;
    }
    if ((char)i == *str)
    {
        return (char*)str;
    }
    return NULL;
}

char * Strchr(char to[], char Ch)
{
    int i = 0;
    while (to[i] != Ch && to[i] != '\0')
        i++;
    return to[i] != '\0' ? to + i : NULL;
}
const char *my_strstr(const char *str, const char *sub_str)
{
    for (int i = 0; str[i] != '\0'; i++)
    {
        int tem = i; //tem保留主串中的起始判断下标位置   
        int j = 0;
        while (str[tem] == sub_str[j])
        {
            if (sub_str[j] == '\0')
            {
                return &str[i];
            }
       tem++;
       j++;
} }
return NULL; }