熟悉 strspn strcspn 函数的使用
来源:互联网 发布:mac dd bs 编辑:程序博客网 时间:2024/05/18 19:35
strspn()函数:计算字符串str中连续有几个字符都属于字符串accept
头文件:#include <string.h>
原型为:size_t strspn(const char *str, const char * accept);
strspn() 从参数 str 字符串的开头计算连续的字符,而这些字符都完全是 accept 所指字符串中的字符。简单的说,若 strspn() 返回的数值为n,则代表字符串 str 开头连续有 n 个字符都是属于字符串 accept 内的字符。
返回字符串 str 开头连续包含字符串 accept 内的字符数目。所以,如果 str 所包含的字符都属于 accept,那么返回 str 的长度;如果 str 的第一个字符不属于 accept,那么返回 0。
注意:检索的字符是区分大小写的。
范例:
#include <stdio.h>#include <stdlib.h>#include <string.h>int main (){ int i; char str[] = "129th"; char accept[] = "1234567890"; i = strspn(str, accept); printf("str 前 %d 个字符都属于 accept\n",i); system("pause"); return 0;}运行结果:
str 前 3 个字符都属于 accept
strcspn()函数:计算字符串str中连续有几个字符都不属于字符串accept
头文件:#inclued<string.h>
原型为:int strcspn(char *str, char *accept);
str、accept为要进行查找的两个字符串。
strcspn() 从字符串 str 的开头计算连续的字符,而这些字符都完全不在字符串 accept 中。简单地说,若 strcspn() 返回的数值为 n,则代表字符串 str 开头连续有 n 个字符都不含字符串 accept 中的字符。
返回字符串 str 开头连续不含字符串 accept 内的字符数目。
注意:如果 str 中的字符都没有在 accept 中出现,那么将返回 atr 的长度;检索的字符是区分大小写的。
范例:返回s1、s2包含的相同字符串的位置。
#include<stdio.h>#include <stdlib.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); system("pause"); return 0;}
运行结果:
The first char both in s1 and s2 is :cThe position in s1 is: 7
- 熟悉 strspn strcspn 函数的使用
- strspn函数 strcspn函数
- strspn函数&&strcspn函数
- 常用的字符串函数 strspn strcspn
- strspn strcspn 函数
- strcspn和strspn函数
- strcspn和strspn函数
- C语言中strspn()函数和strcspn()函数的对比使用
- strspn()函数和strcspn()函数
- strspn & strcspn
- 字符串函数strspn、strcspn和strpbrk
- C和指针之字符串strspn、strcspn函数源代码的实现
- 重写string.h中的字符串操作函数--strspn和strcspn
- strspn和strcspn妙用
- strspn和strcspn妙用
- strspn() and strcspn()
- strspn和strcspn
- strpbrk strspn strcspn
- Android实现连按两次返回键退出
- Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'
- 今日小程序推荐:IN壁纸-你专属壁纸!
- tensorflow CNN卷积神经网络
- C10K问题
- 熟悉 strspn strcspn 函数的使用
- OC语言学习07-属性生成器
- TensorFlow【深度学习】逻辑分类(Logistic Classification)
- k-临近算法
- 快速排序的c++代码实现
- Day1
- STM32——CAN总线系列
- tomcat调试接口
- JAVA中的几个设计模式