• strstr_while模型


     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!='');*/
    24     
    25 
    26     while (p = strstr(p,"abcd"))
    27     {
    28         ncount++;
    29         p = p + strlen("abcd");
    30         if (*p=='')
    31             break;
    32     }
    33 
    34     printf("ncount:%d
    ", ncount);
    35     printf("hello...
    ");
    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)
    ", ret);
    49         return ret;
    50     }
    51 
    52     while (p=strstr(p,sub))
    53     {
    54         tmpCount++;
    55         p = p + strlen(sub);
    56         if (*p=='')
    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:
    ", ret);
    77     
    78     }
    79     printf("ncount:%d
    ", ncount);
    80     
    81     printf("hello...
    ");
    82     system("pause");
    83     return 0;
    84 }
  • 相关阅读:
    个人常用Git操作记录
    JavaScript引擎基本原理:Shapes和Inline Caches
    Promise.then(a, b)与Promise.then(a).catch(b)问题详解
    p标签中的文本换行
    Vue初始化
    关于vue的源码调试
    关于extjs表单布局的几种方式
    sublime text3安装、注册及常用插件
    chrome扩展程序开发
    关于Git的简单使用
  • 原文地址:https://www.cnblogs.com/linst/p/4856738.html
Copyright © 2020-2023  润新知