strspn()函数

  strspn(返回字符串中第一个在指定字符串中出现的字符下标)
  表头文件 #include<string.h>
  定义函数 size_t strspn (const char *s,const char * accept);
  函数说明 strspn()从参数s 字符串的开头计算连续的字符,而这些字符都完全是accept 所指字符串中的字符。简单的说,若strspn()返回的数值为n,则代表字符串s 开头连续有n 个字符都是属于字符串accept内的字符。
  返回值 返回字符串s开头连续包含字符串accept内的字符数目。
  范例
  1 #include <string.h>
  2 #include <stdio.h>
  3 main()
  4 {
  5 char *str="Linux was first developed for 386/486-based pcs.";
  6 printf("%d\n",strspn(str,"Linux"));
  7 printf("%d\n",strspn(str,"/-"));
  8 printf("%d\n",strspn(str,"1234567890"));
  9 }
  运行结果:
  5
  0
  0
  函数原型:
  int strspn(const char *s, const char *accept)
  {
  const char *p;
  const char *a;
  size_t 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;
  }

 

posted @ 2011-01-04 14:04  潜龙9527  阅读(239)  评论(0编辑  收藏  举报