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");
    }
  • 相关阅读:
    c#数据类型
    遮罩层
    图片轮播
    js 获取浏览器高度和宽度值
    bootstrap、jquery
    洛谷P1442 铁球落地 题解
    HDU3016 Man Down 题解
    POJ2892 Tunnel Warfare 题解
    HDU2795 Billboard 题解
    CF920F SUM and REPLACE 题解
  • 原文地址:https://www.cnblogs.com/yaozhenhua/p/9416450.html
Copyright © 2011-2022 走看看