strpbrk

  • Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.
  • The search does not include the terminating null-characters of either strings, but ends there.
  • 在 dest 所指向的空终止字节串中,扫描来自 breakset 所指向的空终止字节串的任何字符,并返回指向该字符的指针。
  • 检索字符串 dest 中第一个匹配字符串 breakset 中字符的字符,不包含空结束字符。也就是说,依次检验字符串 dest 中的字符,当被检验字符在字符串 breakset 中也包含时,则停止检验,并返回该字符位置。
  • 若 dest 或 breakset 不是指向空终止字节字符串的指针则行为未定义。
char* strpbrk( const char* dest, const char* breakset );

Parameters

dest

  • C string to be scanned.
  • 指向要分析的空终止字节字符串的指针

breakset

  • C string containing the characters to match.
  • 指向含要搜索的字符的空终止字节字符串的指针

Return Value

  • A pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if none of the characters of str2 is found in str1 before the terminating null-character.If none of the characters of str2 is present in str1, a null pointer is returned.
  • 指向 dest 中首个亦在 breakset 中的字符的指针,或若这种字符不存在则为空指针。
  • 该函数返回 dest 中第一个匹配字符串 breakset 中字符的字符数,如果未找到字符则返回 NULL。

名称代表“字符串指针打断 (string pointer break) ”,因为它返回指向首个分隔符(“打断”)的指针。

Example

//
// Created by zhangrongxiang on 2018/2/6 11:18
// File strpbrk
// #include <stdio.h>
#include <string.h> //依次检验字符串 str1 中的字符,当被检验字符在字符串 str2 中也包含时,则停止检验,并返回该字符位置。
//检索字符串 str1 中第一个匹配字符串 str2 中字符的字符,不包含空结束字符。
int main() {
int i = 0;
const char str1[] = "abcde2fghi3jk4l";
const char str2[] = "34";
const char *str3 = "Life is short,I use C";
char *ret; ret = strpbrk(str1, str2);
if (ret) {
printf("Number one is : %c\n", *ret);//3
} else {
printf("No");
} ret = strpbrk(str1, str3);
if (ret) {
printf("Number one is : %c\n", *ret);//e
} else {
printf("No");
}
printf("\n");
size_t len = strlen(str1);
char *ch = 0;
int index = 0;
char all[20] = {0}; //获取两个字符串的字符交集
for (; i < len; ++i) {
ret = strpbrk((str1 + sizeof(char) * (index + 1)), str3);
if (ret) {
ch = strrchr(str1, *ret);
index = (int) (ch - str1);
printf("Number one is : %c\n", *ret);
all[i] = *ret;
} else {
printf("No");
}
}
printf("\n");
for (i = 0; i < 20; ++i) {
if (all[i] == 0)
break;
printf("%c ", all[i]); //e f h i
}
printf("\n");
//单词统计
const char *str = "hello world, friend of mine!";
const char *sep = " ,!"; unsigned int cnt = 0;
do {
str = strpbrk(str, sep); // 寻找分隔符
// printf("%s\n",str);
if (str){
str += strspn(str, sep); // 跳过分隔符
printf("%s\n",str);
}
++cnt; // 增加词计数
} while (str && *str); printf("There are %d words\n", cnt); // 5 return 0;
}

文章参考

C 标准库 - string.h之strpbrk使用的更多相关文章

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  4. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...

  5. C 标准库 - string.h之memcpy使用

    memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...

  6. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  7. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  8. C 标准库 - string.h之strlen使用

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

  9. C 标准库 - string.h之strrchr使用

    strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...

随机推荐

  1. node后台启动

    node启动后会占用当前shell 后台启动方式: 1.用forever进行管理 npm install -g forever forever start index.js   2.使用nohub命令 ...

  2. React Native 搭建开发环境

    1.先安装node.js,https://nodejs.org/en/download/ 然后,双击下载好的.msi文件安装即可,安装完成后,打开终端,输出npm -v 即可查看我们刚才安装的node ...

  3. path的用法和所遇错误

    首先上源代码: def _path(route, view, kwargs=None, name=None, Pattern=None): if isinstance(view, (list, tup ...

  4. 2个list取差集

    list操作 element in a list but not in other list,元素在一个list,不在另一个list 在数据量大的时候使用numpy的setdiff1d方法的性能非常好 ...

  5. 洛谷P5245 【模板】多项式快速幂

    题面 传送门 题解 话说现在还用数组写多项式的似乎没几个了-- \[B(x)=A^k(x)\] \[\ln B(x)=k\ln A(x)\] 求个\(\ln\),乘个\(k\),\(\exp\)回去就 ...

  6. mysql数据库表的基本操作sql语句总结

    1,命令行登录命令 mysql -h localhost -u root -p C:\Users\lenovo>mysql -u root -p Enter password: ***** We ...

  7. python 入门级教你如何拿到小姐姐微信

      第一题: 首先错误的思路,首先找出  707829217/2+1 里面的所有奇数,然后再利用两个for,来判断 import math def func_get_prime(n): return ...

  8. [Shell]如何获取Maven工程的project.version信息

    问题: 今天遇到Shell中如何能获取Maven项目工程中的project.version信息的问题 解决方案: 使用Maven的Exec 插件 #! /bin/bash MVN_VERSION=$( ...

  9. JavaWeb学习笔记(二十一)—— 监听器Listener

    一.监听器概述 JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext, HttpSession和 ServletRequest等域对 ...

  10. 【算法笔记】B1044 火星数字

    1044 火星数字 (20 分)   火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, ...