【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