C语言之strspn函数

【FROM MSDN && 百科】

原型: size_t strspn (const char *s,const char * accept);

#include<string.h>

strspn返回s中第一个不在accept中出现的字符下标。

Returns an integer value specifying the length of the substring in str that consists entirely of characters in strCharSet. Ifstr begins with a character not in strCharSet, the function returns 0.

DEMO:自己实现strspn函数

#include <stdio.h>
#include <string.h>
#include <conio.h>
int mystrspn(const char *s,const char *accept);
int main(void)
{
	char *str="hello";
	char *strSet="hl";
	int result;
	result=mystrspn(str,strSet);
	printf("%d\n",result);
	getch();
	return 0;
}
/*from 百科*/
int mystrspn(const char *s,const char *accept)
{
	const char *p;
	const char *a;
	int count=0;
	for (p=s;*p!='\0';p++)
	{
		for (a=accept;*a!='\0';a++)
		{
            if (*p==*a)
            {
				break;
            }
		}
		if (*a=='\0')
		{
			return count;
		}
		++count;
	}
	return count;

}

DEMO:

#define FIRST_DEMO
//#define SECOND_DEMO
#ifdef FIRST_DEMO
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main(void)
{
	char *str="Linux was first developed for 386/486-based pcs.";
	printf("%d\n",strspn(str,"iLnux"));
	printf("%d\n",strspn(str,"inuL"));
	printf("%d\n",strspn(str,"nuxL"));
	printf("%d\n",strspn(str,"inu"));
	printf("%d\n",strspn(str,"nuxil"));
	printf("%d\n",strspn(str,"1234567890"));

	getch();
	return 0;
}
#elif defined SECOND_DEMO
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main(void)
{
	char string[]="cabbage";
	int result;
	result=strspn(string,"abc");
	printf("%d\n",result);
	getch();
	return 0;
}
#endif



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值