ubuntu,debian,redhat,fedora,centos
« »

查找指定字符串在另一字符串中的位置(strpos)

  1. #include "stdio.h"
  2. int main()
  3. {
  4.      char* str = "linuxany.com";
  5.      printf("pos1:%d\n",strpos(str,"any",1));
  6.      printf("pos2:%d\n",strpos(str,"n",2));
  7.      return 0;
  8. }
  9. //函数定义:取字符串位置,中文作为一个字符计算

  1. int strpos(const char* string,const char* find,int number)
  2. {
  3.      char* pos = string;
  4.      char* p = string;
  5.      int count = 0;
  6.      while (number > 0)
  7.      {
  8.         /*定义查找到的字符位置的指针,以便临时指针进行遍历*/
  9.         pos = strstr(p,find);
  10.         /*当位置指针为0时,说明没有找到这个字符*/
  11.         if (pos == 0)
  12.            return -1;
  13.         /*当位置指针和临时指针相等说明下一个字符就是要找的字符,如果临时指针小于位置指针,则进行遍历字符串操作,并将count增1*/
  14.         while(p <= pos)
  15.         {
  16.            if(*p > 0x80 || *p < 0)
  17.            {
  18.               p++;
  19.            }
  20.            p++;
  21.            count++;
  22.         }
  23.         /*对要查找的次数减一*/
  24.         number--;
  25.      }
  26.      return count;
  27. }

您还可能感兴趣的内容

日志信息 »

该日志于2009-12-24 12:32由 admin 发表在C/C++分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »

返回顶部