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

来源:互联网 发布:剑元上至 知乎 编辑:程序博客网 时间:2024/05/16 06:36

函数实现:


信息来自RHEL,man page:STRCHR(3)                                      Linux Programmer's Manual                                     STRCHR(3)NAME       strchr, strrchr, strchrnul - locate character in stringSYNOPSIS       #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);}

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

2 0
原创粉丝点击