zoukankan      html  css  js  c++  java
  • 字符串做函数参数

    #define  _CRT_SECURE_NO_WARNINGS 
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    //char *p = "abcd111122abcd3333322abcd3333322qqq"; 
    //求字符串p中 abcd出现的次数
    //1请自定义函数接口,完成上述需求 50 // i++ ++ ++
    //2 自定义的业务函数 和 main函数必须分开  50
    
    void main81()
    {
        //strstr(str, str2)
        int ncount = 0;
    
        //初始化 让p指针达到查找的条件
        char *p = "11abcd111122abcd3333322abcd3333322qqq";  
    
        do 
        {
            p = strstr(p, "abcd");
            if (p != NULL)
            {
                ncount++; //
                p = p + strlen("abcd"); //指针达到下次查找的条件
            }
            else
            {
                break;
            }
        } while (*p != '');
    
    
        printf("ncount:%d 
    ", ncount);
        system("pause");
        return ;
    }
    
    void main88()
    {
        int ncount = 0;
    
        //初始化 让p指针达到查找的条件
        char *p = "2abcd3333322qqqabcd";  
        while ( p = strstr(p, "abcd"))
        {
            ncount ++;
            p = p + strlen("abcd"); //让p指针达到查找的条件
            if (*p == '')
            {
                break;
            }
        }
        printf("ncount:%d 
    ", ncount);
    
        printf("hello...
    ");
        system("pause");
    }
    
    //char *p = "abcd111122abcd3333322abcd3333322qqq"; 
    //求字符串p中 abcd出现的次数
    //1请自定义函数接口,完成上述需求 50 // i++ ++ ++
    //2 自定义的业务函数 和 main函数必须分开  50
    int getCount(char *mystr /*in*/, char *sub /*in*/,int *ncount)
    {
        int ret = 0;
        int tmpCount = 0;
        //初始化 让p指针达到查找的条件
        char *p = mystr; //不要轻易改变形参的值
    
        if (mystr==NULL || sub==NULL ||ncount==NULL)
        {
            ret = -1;
            printf("func getCount() err:%d (mystr==NULL || sub==NULL ||ncount==NULL) 
    ", ret);
            return ret;
        }
    
        do 
        {
            p = strstr(p, sub);
            if (p != NULL)
            {
                tmpCount++; //
                p = p + strlen(sub); //指针达到下次查找的条件
            }
            else
            {
                break;
            }
        } while (*p != '');
    
        *ncount = tmpCount; //间接赋值是指针存在的最大意义
        return ret;
    }
    
    int main()
    {
        int ret = 0;
        char *p = "abcd111122abcd3333322abcd3333322qqq"; 
        int count = 0;
        char sub[] = "abcd";
    
        ret = getCount(p,sub,  &count);
        if (ret != 0)
        {
            printf("func getCount() err:%d 
    ", ret);
            return ret;
        }
    
        ret = getCount(p,NULL,  &count);
        if (ret != 0)
        {
            printf("func getCount() err:%d 
    ", ret);
            return ret;
        }
        printf("count:%d 
    ", count);
        system("pause");
    }
  • 相关阅读:
    [转] HashMap的工作原理
    [原创] hadoop学习笔记:hadoopWEB监控
    [编辑] 分享一些java视频
    [原创]spring学习笔记:关于springsource-tool-suite插件的安装
    [原创]安装Oracle 11gR2,以及如何在win8下使用plsql develper连接Oracle数据库 ,在这里和大家分享下
    dedecms _ 当前位置问题的代码
    form表单验证
    input 不支持HTML5的placeholder属性
    Dede文章列表
    DEDE首页调用{dede:field.content/}
  • 原文地址:https://www.cnblogs.com/yaozhenhua/p/9416450.html
Copyright © 2011-2022 走看看