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++提供了内敛函数(inline).C ...
- jquery 实现抖动效果
jQuery.fn.shake = function (intShakes /*Amount of shakes*/, intDistance /*Shake distance*/, intDurat ...
- jQuery获取各种input输入框的值
1.获取一组radio单选框的值(name属性为一组的radio的name属性) var q1 = $("input[name=element_name]:checked").va ...
- DateTime.Now.ToString("yyyy/MM/dd") 时间格式化中的MM为什么是大写的?
如果MM是小写,就表示时间里的分钟yyyy-MM-dd HH:mm:ss (年-月-日 时:分:秒) yyyy-MM-dd HH:mm:ss 年-月-日 时:分:秒大写是为了区分“月”与“分” 顺便说 ...
- IIS 7.5 使用appliaction Initialization
https://blogs.msdn.microsoft.com/amol/2013/01/25/application-initialization-ui-for-iis-7-5/ 待续 正在测试 ...
- 第一个.NET Core应用,创建.NET Core命令
打开cmd,依次输入mkdir .project(创建目录),cd .\.project(进入目录),dotnet new(新建初始项目),dotnet restore(还原依赖),dotnet ru ...
- 数据库客户端工具Oracle SQL Developer
Oracle SQL Developer是Oracle官方提供的数据库连接工具.不仅可以连接自己的数据库(Oracle),而且还可以连接多种其他的数据库(比如:Access.MySQL.SQL Ser ...
- 【QTP专题】04_对象及操作方法
本节介绍知识点包括 1.QTP自动化的原理 2.两类对象:TO(测试对象).RO(运行对象) 3.操作方法:SetTOProperty,GetROProperty,GetTOProperty 1.QT ...
- Linux--多用户登录服务器端口抓包
以root身份登录1.新建用户组用命令groupadd test2.添加用户useradd -d /home/test/bei_1 -s /bin/sh -g test -m bei_1此命令新建了一 ...
- 201621123023《Java程序设计》第6周学习总结
一.本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 二.书面作业 1. clone方法 1.1 在te ...