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. 来一个使用sysbench测试cpu性能的简单脚本

    #!/bin/bash for ((i=1; i<16; i++)); do sysbench cpu run --cpu-max-prime=10000 --threads=4 --time= ...

  2. [warn] 7#7: *676 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000007

    nginx 上传文件遇到的Bug. fastcgi_buffer_size 128k; fastcgi_buffers 8 128k; fastcgi_busy_buffers_size 128k; ...

  3. 2018 UESTC 线段树专题

    A - 一棵简单的线段树 A[1...n]初始全为0. 1. 给两个数p 和 x(1≤p≤n),单点更新 A[p] <- x 2. 给两个数L和R (1≤L<R≤n),  L到R区间里这几 ...

  4. Codeforces 387E George and Cards

    George and Cards 我们找到每个要被删的数字左边和右边第一个比它小的没被删的数字的位置.然后从小到大枚举要被删的数, 求答案. #include<bits/stdc++.h> ...

  5. UOJ#275. 【清华集训2016】组合数问题 数位dp

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ275.html 题解 用卢卡斯定理转化成一个 k 进制意义下的数位 dp 即可. 算答案的时候补集转化一下 ...

  6. BZOJ1823 [JSOI2010]满汉全席 2-sat

    原文链接http://www.cnblogs.com/zhouzhendong/p/8125944.html 题目传送门 - BZOJ1823 题意概括 有n道菜,分别可以做成满式和汉式(每道菜只能做 ...

  7. Excel表列名称(给定一个正整数,返回它在 Excel 表中相对应的列名称。)

    例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... 示例 1: 输入: 1 输出: "A ...

  8. CodeSignal 刷题 —— almostIncreasingSequence

    Given a sequence of integers as an array, determine whether it is possible to obtain a strictly incr ...

  9. TensorFlow加载图片的方法

    方法一:直接使用tensorflow提供的函数image = tf.gfile.FastGFile('PATH')来读取一副图片: import matplotlib.pyplot as plt; i ...

  10. [洛谷P1063][NOIP2006]能量项链

    区间DP模板题 题目描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗 ...