Chinaunix首页 | 论坛 | 博客
  • 博客访问: 504887
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1172
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-21 13:40
个人简介

技术改变命运

文章分类

全部博文(184)

文章存档

2020年(16)

2017年(12)

2016年(156)

我的朋友

分类: C/C++

2016-08-08 10:21:49

c语言代码实现:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<string.h>
  3. int searchnum(char *str,char *pattern)
  4. {
  5.         if (str == NULL)
  6.             return 0;
  7.         char *pos = NULL;
  8.         int count = 0;
  9.         while ((pos = strstr(str,pattern)) != NULL)
  10.         {
  11.             count++;
  12.             pos += (strlen(pattern));
  13.             str = pos;
  14.     //        count = count+1;
  15.         }
  16.         return count;
  17. }
  18. int main()
  19. {
  20.     char *str = "world hello world word world";
  21.     printf("%d\n",searchnum(str,"world"));
  22.     return 0;
  23. }
运行结果:
[root@localhost ~]# ./a.out
3

阅读(1373) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~