常用的字符串函数 strspn strcspn

strspn  strcspn

/*
int strspn(const char *, const char *);
strspn函数范例strspn(“abc”, “bcd”),搜索字符串“abc”中
是否有不存在于字符集“bcd”中出现的字符。如„a‟没有出现
在”bcd”中,则函数返回字符串“abc”中„a‟的下标:0,如果
第一个参数代表的字符串中的所有字符都出现在第二个参数
代表的字符串中则函数返回'\0'字符的下标:3。
*/
/*
strcspn函数范例strcspn(“abc”, “bcd”),搜索字符串“abc”
中出现在”bcd”字符集中的字符返回其下标,则函数应该返
回“abc”中„b‟的下标:1;若第一个参数中所有字符都没有
在第二个参数中出现则返回„\0'的下标。
*/


#include <stdio.h>
#include <string.h>
//#include <strlib.h>

int main(void)
{
	int t;
	char str1[20];
	char str2[20];
	gets(str1);
	gets(str2);
	t = strspn(str1, str2);
	printf("strspn:%d\n", t);

	t = strcspn(str1, str2);
	printf("strcspn:%d\n", t);
	return 0;
}

/*
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./strspn 
abc 
bcd
strspn:0
strcspn:1
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./strspn 
abc
abc
strspn:3
strcspn:0
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./strspn 
abc 
bcd
strspn:0
strcspn:1
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./strspn 
abc
def
strspn:0
strcspn:3

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值