• strtok strchr strrchr strchrnul


    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 '', 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、判断当前待分解的位置是否为'',若是则返回NULL,否则继续
        if(*str_arr =='')
        {
            return NULL;
        }
        /*
        * 4、保存当前的待分解串的指针b_temp,调用strpbrk()在b_temp中找分解符,
        * 如果找不到,则将temp_str赋值为待分解字符串末尾部''的位置,
        * b_temp没有发生变化;若找到则将分解符所在位置赋值为'',
        * b_temp相当于被截断了,temp_str指向分解符的下一位置。
        */
        b_temp = str_arr;
        str_arr = strpbrk(str_arr, delimiters);
        if(str_arr == NULL)
        {
            *temp_str = strchr(b_temp,'');
        }
        else
        {
            *str_arr ='';
            *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;
    }

  • 相关阅读:
    DEM地形渲染中光源的方向
    python GDAL安装运行后提示找不到proj.db
    AO——将函数栅格数据集保存到磁盘
    arcgis影像分幅图裁剪
    arcgis判断线是否穿过面
    WSL 使用vscode写python
    python-gdal支持filegdb读写
    Chapter5 生长因子、受体和癌症
    Chapter6 胞内信号网络
    【转】B树、B+树、B*树
  • 原文地址:https://www.cnblogs.com/henryliublog/p/9098815.html
Copyright © 2020-2023  润新知