C庫中重要字串函式strspn與strcspn
阿新 • • 發佈:2018-12-23
(1)函式strspn:
size_t strspn ( const char * str1, const char * str2 );
功 能: 在串中查詢指定字符集的子集的第一次出現,如果str1中的所有字元都在str2中出現過,那麼返回str1的長度。如果第一個字元就不在str2中,那麼返回0.
#include <stdio.h> #include <string.h> int main() { int i; const char *cset="1234567890"; char *str="129thfangzhen33332423"; i=strspn(str,cset); //i=3 i=strspn(str+5,cset); //i=0 printf("%d\n",i); return 0; }
(2)函式strcspn:
size_t strcspn ( const char * str1, const char * str2 );
功 能: 在串中查詢第一個給定字符集內容的段的開始位置。
對於下面的程式:
#include <stdio.h> #include <string.h> int main(){ int i; const char *cset="1234567890"; char *str="129thfangzhen33332423"; i=strcspn(str,cset); printf("%d\n",i); //i=0 i=strcspn(str+5,cset);//i=8 printf("%d\n",i); return 0; }