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.

Strtok()函数详解:

  该函数包含在"string.h"头文件中
函数原型:

  1. char* strtok (char* str,constchar* delimiters );

函数功能:
  切割字符串,将str切分成一个个子串
函数参数:
  str:在第一次被调用的时间str是传入需要被切割字符串的首地址;在后面调用的时间传入NULL。
  delimiters:表示切割字符串(字符串中每个字符都会 当作分割符)。
函数返回值:
  当s中的字符查找到末尾时,返回NULL;
  如果查不到delimiter所标示的字符,则返回当前strtok的字符串的指针。

#include<stdio.h>
#include<string.h>
int main(void)
{
    char buf[]="hello@boy@this@is@heima";
    char*temp = strtok(buf,"@");
    while(temp)
    {
        printf("%s ",temp);
        temp = strtok(NULL,"@");
    }
    return0;
}

=======>"hello boy this is heima "

自己实现strtok()函数

#include<stdio.h>
#include<string.h>
//根据函数原型实现strtok()函数
char* myStrtok_origin(char* str_arr,constchar* delimiters,char**temp_str)
{
    //定义一个指针来指向待分解串
    char*b_temp;
    /*
    * 1、判断参数str_arr是否为空,如果是NULL就以传递进来的temp_str作为起始位置;
    * 若不是NULL,则以str为起始位置开始切分。
    */
    if(str_arr == NULL)
    {
        str_arr =*temp_str;
    }
    //2、跳过待分解字符串
    //扫描delimiters字符开始的所有分解符
    str_arr += strspn(str_arr, delimiters);
    //3、判断当前待分解的位置是否为'\0',若是则返回NULL,否则继续
    if(*str_arr =='\0')
    {
        return NULL;
    }
    /*
    * 4、保存当前的待分解串的指针b_temp,调用strpbrk()在b_temp中找分解符,
    * 如果找不到,则将temp_str赋值为待分解字符串末尾部'\0'的位置,
    * b_temp没有发生变化;若找到则将分解符所在位置赋值为'\0',
    * b_temp相当于被截断了,temp_str指向分解符的下一位置。
    */
    b_temp = str_arr;
    str_arr = strpbrk(str_arr, delimiters);
    if(str_arr == NULL)
    {
        *temp_str = strchr(b_temp,'\0');
    }
    else
    {
        *str_arr ='\0';
        *temp_str = str_arr +1;
    }
    //5、函数最后部分无论找没找到分解符,都将b_temp返回。
    return b_temp;
}
//使用myStrtok来简化myStrtok_origin函数
char* myStrtok(char* str_arr,constchar* delimiters)
{
    staticchar*last;
    return myStrtok_origin(str_arr, delimiters,&last);
}
int main(void)
{
    char buf[]="hello@boy@this@is@heima";
    //1、使用myStrtok_origin()函数
    char*temp_str = NULL;
    char*str = myStrtok_origin(buf,"@",&temp_str);
    while(str)
    {
        printf("%s ",str);
        str = myStrtok_origin(NULL,"@",&temp_str);
    }
    //2、使用myStrtok()函数
    char*str1 = myStrtok(buf,"@");
    while(str1)
    {
        printf("%s ",str1);
        str1 = myStrtok(NULL,"@");
    }
    return0;
}

strtok strchr strrchr strchrnul的更多相关文章

  1. 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset

    bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...

  2. strstr strchr strrchr

    通过函数的定义来区分: 1.strstr: 返回子串出现的第一次位置 char *strstr(const char *haystack, const char *needle) 可见,strstr函 ...

  3. PHP字符串函数之 strstr stristr strchr strrchr

    strstr -- 查找字符串的首次出现,返回字符串从第一次出现的位置开始到该字符串的结尾或开始. stristr -- strstr 函数的忽略大小写版本 strchr -- strstr 函数的别 ...

  4. hdu1106 字符串水题strtok()&&strchr()&&sscanf()+atoi()使用

    字符串的题目 用库函数往往能大大简化代码量 以hdu1106为例 函数介绍 strtok() 原型: char *strtok(char s[], const char *delim); 功能: 分解 ...

  5. strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置

    在一个较长的字符串这查找匹配的字符串或字符,其中strstr()和strchr()是完全一样的. 例: echo strstr('why always you','you'); 输出: you 如果为 ...

  6. strstr strchr strrchr strrstr

    通过函数的定义来区分: 1.strstr: 返回子串出现的第一次位置 char *strstr(const char *haystack, const char *needle) 可见,strstr函 ...

  7. C string.h 常用函数

    参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...

  8. Cdev

    1,#和##操作符Operator,使用 首个参数返回为一个带引号的字符串 predefined variable was not declared in the scope;

  9. PHP 12 :字符串的操作

    原文:PHP 12 :字符串的操作 本章介绍字符串的操作.之所以要把字符串单独拿出来讲,是因为字符串在每种语言里都是非常重要的.并且也是大家关心的.我们从以下几个方面介绍字符串: 字符串的表现形式. ...

随机推荐

  1. 使用Docker方式运行Mysql(MariaDB)

    两者差不多.我使用的是MariaDB. 下面的docker命令,挂了数据,配置,映射了端口,指定了root密码,服务端编码. 蛮快的! docker run \ --name mariadb \ -v ...

  2. mysql存储过程的学习(mysql提高执行效率之进阶过程)

    1:存储过程: 答:存储过程是sql语句和控制语句的预编译集合,以一个名称存储并作为一个单元处理:存储过程存储在数据库内,可以由应用程序调用执行,而且允许用户声明变量以及进行流程控制,存储类型可以接受 ...

  3. Linux系统监控命令及定位Java线程

    1.PID.TID的区分 uid是user id,即用户id,root用户的uid是0,0为最高权限,gid是group id,用户组id,使用 id 命令可以很简单的通过用户名查看UID.GID:~ ...

  4. 选择结构if

    1.if语句 if语句是指如果满足某种条件,就进行某种处理.例如,小明妈妈跟小明说“如果你考试得了100分,星期天就带你去游乐场玩”.这句话可以通过下面的一段伪代码来描述. 如果小明考试得了100分 ...

  5. Note for "Some Remarks on Writing Mathematical Proofs"

    John M. Lee is a famous mathematician, who bears the reputation of writing the classical book " ...

  6. js压缩图片上传插件localResizeIMG

    示例 /** * 本地图片压缩后上传 */ $("#vfile").change(function(){ var _this = $(this); lrz(this.files[0 ...

  7. ZJOI2018 胖 二分 ST表

    原文链接https://www.cnblogs.com/zhouzhendong/p/ZJOI2018Day2T2.html 题目传送门 - BZOJ5308 题目传送门 - LOJ2529 题目传送 ...

  8. windows下编译php7图形库php_ui.dll

    CSDN博客 具有图形化编程才有意思,这几天看到了php ui 图形扩展,只是现在只能下载php 7.1的 本次教程编译php7.2.6的 php ui 要是linux下编译起来比较简单 但是 win ...

  9. a标签下划线

    页面中有一处box中的a标签都被加上了下划线,查找元素却没有找到css中的underline. 原因是 <a>标签默认是有下划线的,而一般看到的<a>标签链接中的下划线都被覆盖 ...

  10. Java设计模式从精通到入门四 工厂方法模式

    工厂方法模式 属于23中设计模式中创建型类型. 核心思想:工厂提供创建对象的接口,由子类决定实例化哪一个子类. 来源 ​ 设计模式之禅中的例子,女娲造人,通过八卦炉来进行造人,没有烧熟的为白人,烧太熟 ...