码迷,mamicode.com
首页 > 其他好文 > 详细

strstr_while模型

时间:2015-10-06 10:17:47      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 int method()
 7 {
 8     char *p = "123abcd23sdfwee131abcd12344abcd";
 9     int ncount = 0;
10     
11     /*do
12     {
13     p = strstr(p, "abcd");
14     if (p != NULL)
15     {
16     ncount++;
17     p = p + strlen("abcd");
18     }
19     else
20     {
21     break;
22     }
23     } while (*p!=‘\0‘);*/
24     
25 
26     while (p = strstr(p,"abcd"))
27     {
28         ncount++;
29         p = p + strlen("abcd");
30         if (*p==\0)
31             break;
32     }
33 
34     printf("ncount:%d\n", ncount);
35     printf("hello...\n");
36     system("pause");
37     return 0;
38 }
39 int getCount(char *mystr/*in*/,char *sub/*in*/, int *ncount)
40 {
41     int ret = 0;
42     int tmpCount = 0;
43     //初始化让p指针到查找的条件
44     char *p = mystr;    //不要轻易改变形参的值
45     if (mystr == NULL || sub == NULL || ncount == NULL)
46     {
47         ret = -1;
48         printf("func getCount() err:%d(mystr == NULL || sub || NULL || ncount == NULL)\n", ret);
49         return ret;
50     }
51 
52     while (p=strstr(p,sub))
53     {
54         tmpCount++;
55         p = p + strlen(sub);
56         if (*p==\0)
57             break;
58     }
59     *ncount = tmpCount;    //间接赋值是指针存在的意义
60     return ret;
61 }
62 
63 int main()
64 {
65     char *p = "abcd1234asd222abcd";
66     /*char *sub;
67     sub = "abcd";*/
68     char sub[] = "abcd";
69     int ncount=0;
70     int ret = 0;
71     
72     
73     ret = getCount(p,sub,&ncount);
74     if (ret != 0)
75     {
76         printf("func getCount() err:\n", ret);
77     
78     }
79     printf("ncount:%d\n", ncount);
80     
81     printf("hello...\n");
82     system("pause");
83     return 0;
84 }

 

strstr_while模型

标签:

原文地址:http://www.cnblogs.com/linst/p/4856738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!