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 ...
随机推荐
- 安装pygame for Python3.5
首先进入pygame官网下载页面:http://www.pygame.org/download.shtml 页面上提供的有py2.4-py2.7,py3.1-py3.2的版本(py2.4就是对应py ...
- CentOS安装gotop
1.安装go语言环境 yum install golang 2.安装gotop程序 git clone --depth 1 https://github.com/cjbassi/gotop /tmp/ ...
- Arduino Nano 读取ADS1100实例
利用Arduino Nano的wire库可以很方便对ADS1100进行设置和读取转换后的数据. /* * Arduino reads ADS1100 I2C 16bit diff ADC */ /* ...
- 两款Mongodb可视化工具
在某些场景下,相比传统的关系数据库和NoSQL数据库,Mongodb拥有不可替代的优势. 例如,最近我需要为收集的大量网站进行分类.实际情况是,一个网站可能同时有多个标签,想象一下新浪网,它既是门户站 ...
- IoC概要
控制反转基本上说的是功能调用者与功能实现者之间应该如何交互,即二者之间没有直接的强耦合(调用者new一个被调用者),而是都依赖同一个抽象,这个抽象规定了二者交互的接口.反转的意思是实现了依赖倒置,在程 ...
- hdu3089 Josephus again|快速约瑟夫环
题目链接:戳我 貌似是高一昨天的考试题T2?????感觉挺好玩的就搞了搞qwqwq 其实是HDU上面的题啦.... 对于普通的约瑟夫问题,大概是n个人围成一个环,从1开始报数,数到k,那个人出队,最后 ...
- Ubuntu16.04以root身份登入!
首先以非root用户身份登入系统. 1,修改root密码:启动shell,随后在shell里面输入命令: sudo passwd root 最后输入root要使用的密码,需要输入两次,这样root密码 ...
- day03.1-函数编程
python中函数的定义: def test (x,y): "The function definitions" z = x**y return z ""&qu ...
- AHOI2005航线规划 bzoj1969(LCT缩点)
题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由千百万星球构成的Samuel星系. 星际空间站的Samuel II巨型计算 ...
- 【SSO单点系列】开篇
年底将至,忙碌了好几个月的项目也接近尾声了.在这个项目中,由于要和其他外系统做单点登录(SSO),整合其他系统的功能.在网上查询了相关资料后,最终选取了Yale大学发起的一个开源项目 CAS, 作为项 ...