C 标准库 - string.h之strpbrk使用
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;
}
文章参考
- http://zh.cppreference.com/w/c/string/byte/strpbrk
- http://www.cplusplus.com/reference/cstring/strpbrk/
- http://www.runoob.com/cprogramming/c-function-strpbrk.html
C 标准库 - string.h之strpbrk使用的更多相关文章
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- C标准库<string.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...
- C标准库string.h中几个常用函数的使用详解
strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...
- C 标准库 - string.h之memmove使用
memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...
- C 标准库 - string.h之memcpy使用
memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...
- C 标准库 - string.h之memcmp使用
memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...
- C 标准库 - string.h之memchr使用
memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...
- C 标准库 - string.h之strlen使用
strlen Returns the length of the C string str. The length of a C string is determined by the termina ...
- C 标准库 - string.h之strrchr使用
strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...
随机推荐
- yum使用,使用rpm指令安装rpm,使用dpkg指令安装deb
yum安装时如果报错提示安装失败,缺少库文件,可以使用: yum whatprovides 库名 之后安装提供的程序 yum remove xxx卸载 yum update 更新 解决 Require ...
- SVN检出忽略文件夹文件
具体实现:1.在解决方案目录上点右键2.在乌龟SVN菜单中找到"属性"点开 3.在弹出窗中点 新建--其他 4.在弹出窗中的"属性"中选择"svn: ...
- CentOS7 关闭防火墙[转]
CentOS6关闭防火墙使用以下命令, /临时关闭 service iptables stop //禁止开机启动 chkconfig iptables off CentOS7中若使用同样的命令会报错, ...
- .NET CORE 2.1 导出excel文件的两种方法
最近在做 MVC 项目的时候遇到项目的导出,下面总结下两种导出到excel 的方法 第一种方法: 将文件写到本地,然后返回这个File 或者返回这个 File 的绝对地址 其中 _hostingE ...
- C#连接SQL server数据库
C#连接SQL server数据库 创建一个Windows应用程序,在窗体中添加TextBox控件.Button控件.Label控件. private void button1_Click(objec ...
- [C#]合并单元格(行、列)
详细链接:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeak説明:控件ID指的是页面上面的Grid ...
- 牛客网提高组模拟赛第七场 T3 洞穴(附bitset介绍)
就是DP. 我们可以很简单的想到要枚举中间点,进行边数的转移. 但是因为边长数据范围很大,所以我们考虑log的倍增. 状态设计为\(dp[i][j][k]\),为从节点\(i\)走\(2^k\)步能否 ...
- 二十一、Node.js-EJS 模块引擎
初识 EJS 模块引擎 我们学的 EJS 是后台模板,可以把我们数据库和文件读取的数据显示到 Html 页面上面.它是一个第三方模块,需要通过 npm 安装https://www.npmjs.com/ ...
- 该用li还是得用
如:这样子少一层是很好,但为了美观,后面都补上a,但鼠标经过也会有背景色,所以还是得多一层li 解决后:
- [Swift实际操作]八、实用进阶-(4)通过protocol在两个对象中进行消息传递
本文将演示如何借助协议,实现视图控制器对象和其内部的自定义视图对象之间的数据传递. 首先创建一个自定义视图对象.在项目名称文件夹点击鼠标右键New File ->Cocoa Touch Clas ...