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. CentOS6.9安装Kafka

    先设置jdk1.8 vi /etc/profile export JAVA_HOME=/usr/local/jdkexport JRE_HOME=/usr/local/jdk/jreexport CL ...

  2. [转]MySQL忘记密码的正确解决方法

    http://database.51cto.com/art/201005/201986.htm 以下的文章主要介绍的是MySQL忘记密码的正确解决方法,在实际操作中如果你忘记MySQL密码是一件很头痛 ...

  3. [转]基于国家标准的 EndNote 输出样式模板 ----直接用endnote导入到word,不用自己一个个改参考文献了

    EndNote 相当于一个数据库,将添加/导入的文献存档.需要引用文献的时候就从中选择一个插入到文档中,EndNote 会自动给你编号.在文档末尾建立相应的参考文献列表.但是各种杂志.单位要求的文献著 ...

  4. MUI组件四:选择器、滚动条、单选框、区域滚动和轮播组件

    目录(?)[+]   1.picker(选择器) mui框架扩展了pipcker组件,可用于弹出选择器,在各平台上都有统一表现.poppicker和dtpicker是对picker的具体实现.*pop ...

  5. 【AtCoder】ARC072

    ARC072 C - Sequence 直接认为一个数是正的,或者第一个数是负的,每次将不合法的负数前缀和改成+1正数前缀和改成-1 #include <bits/stdc++.h> #d ...

  6. python全栈开发day56-mysql

    1.数据库和表 show总结 SHOW DATABASES;返回可用数据库的一个列表. SHOW TABLES;返回当前选择的数据库内可用表的列表. SHOW COLUMNS FROM custome ...

  7. ionic2中使用moment.js

    安装 npm i moment --save 使用 import { Pipe, PipeTransform } from '@angular/core'; import Moment from 'm ...

  8. 牛客挑战赛30 小G砍树 树形dp

    小G砍树 dfs两次, dp出每个点作为最后一个点的方案数. #include<bits/stdc++.h> #define LL long long #define fi first # ...

  9. Python上下文管理器 with

    对于系统资源的操作,如:文件操作.数据库操作等,我们往往打开文件.连接数据库后忘了将其close掉,这时就可能会引发异常,因此我们常用的做法是: # coding:utf-8 f = open(&qu ...

  10. 001.Ceph简介概述

    一 Ceph简介 Red Hat Ceph是一个分布式的数据对象存储,系统设计旨在性能.可靠性和可扩展性上能够提供优秀的存储服务.分布式对象存储是存储的未来,因为它们适应非结构化数据,并且客户端可以同 ...