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 ...
随机推荐
- C++ 调用C++写的类库的2种方法之一(隐式链接)
一:创建C++ DLL类库,名称:Dll1 1.Dll1.h /*#ifndef Dll_API #else #define Dll_API _declspec(dllimport) #endif * ...
- Replication--Alwayson+订阅
场景: 主服务器:SubServer1 从服务器:SubServer2 监听者:RepListener 分发库:DisServer 发布数据库:RepDB1 发布服务器:RepServer1 当fai ...
- 菜鸟的Xamarin.Forms前行之路——原生Toast的简单实现方法
项目中信息提示框,貌似只有个DisplayAlert,信息提示太过于单一,且在有些场合Toast更加实用,以下是一个简单的原生Toast的实现方法 项目地址:https://github.com/we ...
- 在Linux下启动Java服务的脚本
#!/bin/sh #该脚本为Linux下启动java程序的通用脚本.即可以作为开机自启动service脚本被调用, #也可以作为启动java程序的独立脚本来使用. # #Author: tudaxi ...
- IE 直接打印 页面的方式
1. 通过在IE中加载adobe pdf reader 插件 进行直接打印和打印的配置(推荐) <body> <object id="pdf1" width= ...
- tail()和head()
pandas 里的tail() 函数,读取后几行. head() dataframe.head():前五行 显示某一列的前五行,两种方法: 或者:
- lua 5.3 英文手册 自己收集整理版
/* ** state manipulation */ LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);//创建lua虚拟机 LUA ...
- bzoj1800飞行棋
题目链接 简单模拟 真的不敢相信ahoi09年的题竟然是这个难度 首先,一个必须要知道的定理是:圆上只有直径所对的圆周角是直角. 然后就很显然了, 只有两条不重合的直径上的四个点才能组成一个矩形, 所 ...
- mysqli扩展库---------预处理技术
1, PHP程序与mysql之间处理sql语句流程如下,减少执行时间方式有三种: ① 减少php发送sql次数: ② 减少php与mysql之间网络传输时间: ③ 减少mysql的编译时间: 2, 预 ...
- 6,synchronized, lock 区别
参考文档 http://zzhonghe.iteye.com/blog/826162 http://houlinyan.iteye.com/blog/1112535 1,ReentrantLock 拥 ...