strchr, strrchr函数实现——string.h库函数


函数实现:


信息来自RHEL,man page:
STRCHR(3)                                      Linux Programmer's Manual                                     STRCHR(3)

NAME
       strchr, strrchr, strchrnul - locate character in string

SYNOPSIS
       #include <string.h>

       char *strchr(const char *s, int c);

       char *strrchr(const char *s, int c);

       #define _GNU_SOURCE         /* See feature_test_macros(7) */
       #include <string.h>

       char *strchrnul(const char *s, int c);

DESCRIPTION
       The strchr() function returns a pointer to the first occurrence of the character c in the string s.

       The strrchr() function returns a pointer to the last occurrence of the character c in the string s.

       The  strchrnul() function is like strchr() except that if c is not found in s, then it returns a pointer to the
       null byte at the end of s, rather than NULL.

       Here "character" means "byte"; these functions do not work with wide or multibyte characters.

RETURN VALUE
       The strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is  not
       found.   The  terminating  null byte is considered part of the string, so that if c is specified as '\0', these
       functions return a pointer to the terminator.

       The strchrnul() function returns a pointer to the matched character, or a pointer to the null byte at  the  end of s (i.e., s+strlen(s)) if the character is not found.

VERSIONS
       strchrnul() first appeared in glibc in version 2.1.1.

CONFORMING TO
       strchr() and strrchr() are in SVr4, 4.3BSD, C89, C99.  strchrnul() is a GNU extension.

strchr()函数实现:


0.函数原型:

#include <string.h>

char *strchr(const char *s, int c);

1.参数:

1.s:指定字符串指针。
2.c:欲查询字符’c’。

2.返回值:

如果字符串s中:

->含有'c'字符,那么返指向回字符'c'第一次出现位置的指针
->如果没有,返回NULL。

3.功能描述:

strchr函数在指定字符串s中查找字符’c’,并返回指向该字符在字符串中第一次出现位置的指针。没找到则返回NULL。

4.实现:


char *my_strchr(const char *s, int c)
{
    char *s_func = (char *)s;

    //参数判断
    if(NULL == s_func){
        return NULL;
    }

    //具体实现
    while(*s_func && *s_func != c){
        ++s_func;
    }

    return (s_func ? s_func : NULL);
}

strrchr()函数实现:


0.函数原型:

#include <string.h>

char *strrchr(const char *s, int c);

1.参数:

同strchr函数参数。

2.返回值:

如果字符串s中(区别与strchr函数的是,strrchr函数查找的是c最后一次出现的位置):

->含有'c'字符,那么返指向回字符'c'最后一次出现位置的指针
->如果没有,返回NULL。。

3.功能描述:

strrchr函数参照strchr函数功能,区别在于该函数查询c在字符串中最后一次出现的位置。

4.实现:


实现一:
char *my_strrchr1(const char *s, int c)
{
    char *s_func = (char *)s;
    char *temp   = NULL;

    //参数判断
    if(NULL == s_func){
        return NULL;
    }

    //temp找尾,s_func截头
    temp = s_func;        //先将temp指向s
    while(*temp){         //将temp指向s末尾
        ++temp;
    }

    while(temp != s_func && *temp != c){    //再让temp指针往回跑,边跑遍判断是不是找到c
        --temp;
    }

    //如果temp又回到了s的开头,说明没找到,返回NULL,否则返回temp
    return (temp != s_func ? temp : NULL);
}
实现二:
char *my_strrchr2(const char *s, int c)
{
    char *s_func = (char *)s;
    char *result = NULL     ;

    //参数判断
    if(NULL == s_func){
        return NULL;
    }

    //s_func从前往后找,result记录最后一次指针
    while(*s_func){           //s_func不指向空时循环判断
        if(*s_func == c){     //如果s_func指向c字符
            result = s_func;  //将当前位置记录到result中
        }
        ++s_func;
    }

    return (result ? result : NULL);
}

=============文章结束==============
小菜总结,如有不当,欢迎批评!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值