字符串函数strspn、strcspn和strpbrk

字符串函数strspn、strcspn和strpbrkstrspn 函数头文件 include 函数原型 size tstrspn constchar str constchar strCharSet 参数 str strCharSet 为要进行查找的

大家好,欢迎来到IT知识分享网。

strspn函数

头文件#include <string.h>
函数原型size_t strspn( const char *str, const char *strCharSet );
参数:str、strCharSet 为要进行查找的两个字符串。
返回值:返回字符串 str 开头连续包含字符串 strCharSet内的字符数目。所以,如果 str 所包含的字符都属于strCharSet,那么返回 str 的长度;如果 str 的第一个字符不属于 strCharSet,那么返回 0。
函数说明: strspn() 函数用来计算字符串 string 中连续有几个字符都属于字符串 strCharSet。
strspn() 从参数 str 字符串的开头计算连续的字符,而这些字符都完全是 strCharSet 所指字符串中的字符。简单的说,若 strspn() 返回的数值为n,则代表字符串 str 开头连续有 n 个字符都是属于字符串 strCharSet 内的字符。
注意:检索的字符是区分大小写的。
提示:提示:函数 strcspn() 的含义与 strspn() 相反,可以对比学习。
示例
例1:








#include <stdio.h> #include <string.h> int main () { int i; char str[] = "129th"; char strCharSet[] = ""; i = strspn(str, strCharSet); printf("str 前 %d 个字符都属于 strCharSet\n",i); return 0; }

输出结果
str 前 3 个字符都属于 strCharSet

例2:

#include <string.h> #include <stdio.h> int main( void ) { char string[] = "cabbage"; int result; result = strspn( string, "abc" ); printf( "The portion of '%s' containing only a, b, or c " "is %d bytes long\n", string, result ); printf("string:%s\n",string+result); return 0; }

输出结果
The portion of ‘cabbage’ containing only a, b, or c is 5 bytes long
string:ge

自己实现strspn函数
strspn()函数从名称上来讲属于字符串查找,但实际应用中发现strspn()函数用来在一个字符串中从首地址开始向后递增排查”添加的参数”在这个字符串的范围是多少个字节。
函数声明如下

size_t strspn(const char *str, const char *strCharSet);

函数定义和实现如下

size_t my_strspn(const char *str, const char *strChaSet) { const char *p; const char *s; size_t count = 0; for (p = str; *p != '\0'; ++p) { for (s = strChaSet; *s != '\0'; ++s) { if (*p == *s) break; } if (*s == '\0') return count; ++count; } return count; }

strcspn函数

头文件#include <string.h>
函数原型size_t strcspn( const char *string, const char *strCharSet );
参数:str、strCharSet 为要进行查找的两个字符串。
返回值:返回字符串 str 开头连续不含字符串 strCharSet内的字符数目。所以,如果 str 所包含的字符都不属于strCharSet,那么返回 str 的长度;如果 str 的第一个字符属于 strCharSet,那么返回 0。
函数说明:strcspn() 用来计算字符串 string 中连续有几个字符都不属于字符串 strCharSet。
strcspn() 从字符串 str 的开头计算连续的字符,而这些字符都完全不在字符串 strCharSet 中。简单地说,若 strcspn() 返回的数值为 n,则代表字符串 str 开头连续有 n 个字符都不含字符串 strCharSet 中的字符。
示例
例1:






#include <stdio.h> #include <string.h> int main() { char* s1 = "http://c.biancheng.net/cpp/u/biaozhunku/"; char* s2 = "c is good"; int n = strcspn(s1,s2); printf("The first char both in s1 and s2 is :%c\n",s1[n]); printf("The position in s1 is: %d\n",n); return 0; }

输出结果
The first char both in s1 and s2 is :c
The position in s1 is: 7

例2:判断两个字符串的字符是否有重复的。

#include <stdio.h> #include <string.h> int main() { char* s1 = "http://c.biancheng.net/cpp/xitong/"; char* s2 = "z -+*"; if(strlen(s1) == strcspn(s1,s2)){ printf("s1 is diffrent from s2!\n"); }else{ printf("There is at least one same character in s1 and s2!\n"); } return 0; }

输出结果
s1 is diffrent from s2!

strpbrk函数

头文件#include <string.h>
函数原型char *strpbrk( const char *str, const char *strCharSet );
参数
str – 要被检索的 C 字符串。
strCharSet – 该字符串包含了要在 str 中进行匹配的字符列表。
返回值:该函数返回 str中第一个匹配字符串 strCharSet中字符的字符位置,如果未找到字符则返回 NULL。
函数说明: strpbrk函数用来检索字符串 str中第一个匹配字符串 strCharSet 中字符的字符,不包含空结束字符。也就是说,依次检验字符串 str 中的字符,当被检验字符在字符串 strCharSet 中也包含时,则停止检验,并返回该字符位置。
示例
例1:







int main() { const char str[] = "abcde2fghi3jk4l"; const char strCharSet[] = "34"; char *ret; ret = strpbrk(str, strCharSet); if(ret) { printf("第一个匹配的字符是:%c\n", *ret); } else { printf("未找到字符"); } return(0); }

输出结果
第一个匹配的字符是:3

例2:

int main( void ) { char str[100] = "The 3 men and 2 boys eat 5 pigs\n"; char strCharSet[100] = "0"; char *result; printf( "1: %s\n", str ); result = strpbrk( str, strCharSet ); printf( "2: %s\n", result++ ); result = strpbrk( result, strCharSet ); printf( "3: %s\n", result++ ); result = strpbrk( result, strCharSet ); printf( "4: %s\n", result ); return 0; }

输出结果
1: The 3 men and 2 boys eat 5 pigs

2: 3 men and 2 boys eat 5 pigs

3: 2 boys eat 5 pigs

4: 5 pigs

自己实现strpbrk函数
函数的声明

char * my_strpbrk(const char *str,const char *strCharSet);

函数的定义和实现

char * my_strpbrk(const char *str,const char *strCharSet) { const char *c = strCharSet; if (!*str) return (char *) NULL; while (*str != '\0') { for (c = strCharSet; *c != '\0'; c++) { if (*str == *c) break; } if (*c != '\0') break; str++; } if (*c == '\0') str = NULL; return (char *) str; }

参考:http://www.jb51.net/article/71441.htm
字符串函数strspn、strcspn和strpbrk
http://blog.chinaunix.net/uid-20480343-id-1941643.html

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/113229.html

(0)
上一篇 2025-12-16 07:45
下一篇 2025-12-16 08:10

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信