程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> strstr_while模型,strstr

strstr_while模型,strstr

編輯:關於C語言

strstr_while模型,strstr


 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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved