查找指定字符串在某个字符串中的出现次数

来源:互联网 发布:魔兽世界掉落数据库 编辑:程序博客网 时间:2024/06/07 03:35
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

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(278) | 评论(0) | 转发(0) |
0

上一篇:Linux多线程2-2_线程的生命周期

下一篇:协同进程

相关热门文章
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • 彻底搞定C语言指针详解-完整版...
给主人留下些什么吧!~~