zoukankan      html  css  js  c++  java
  • 两个辅助指针变量挖字符串

    #define  _CRT_SECURE_NO_WARNINGS 
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    //有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";)
    
    
    //分清楚赋值指针变量 和 操作逻辑之间的关系
    int spitString(const char *buf1, char c, char buf2[10][30], int *count)
    {
        //strcpy(buf2[0], "aaaaa");
        //strcpy(buf2[1], "bbbbbb");
        char *p=NULL, *pTmp = NULL;
        int    tmpcount = 0;
    
        //1 p和ptmp初始化
        p = buf1;
        pTmp = buf1;
    
        do 
        {
            //2 检索符合条件的位置 p后移  形成差值 挖字符串
            p = strchr(p, c);
            if (p != NULL)
            {
                if (p-pTmp > 0)
                {
                    strncpy(buf2[tmpcount], pTmp,  p-pTmp);
                    buf2[tmpcount][p-pTmp]  = '';  //把第一行数据变成 C风格字符串
                    tmpcount ++;
                    //3重新 让p和ptmp达到下一次检索的条件
                    pTmp = p = p + 1;
                }
            }
            else
            {
                break;
            }
        } while (*p!='');
        
        *count = tmpcount;
        return 0;
    }
    
    void main()
    {
        int ret = 0, i = 0;
        char *p1 = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";
        char cTem= ',';
        int nCount;
    
        char myArray[10][30];
    
        ret = spitString(p1, cTem, myArray, &nCount);
        if (ret != 0)
        {
            printf("fucn spitString() err: %d 
    ", ret);
            return ret;
        }
    
        for (i=0; i<nCount; i++ )
        {
            printf("%s 
    ", myArray[i]);
        }
        printf("hello...
    ");
        system("pause");
        return ;
    }
    
    
    
    //作业 用第三种内存模型求解问题
    /*
    int spitString2(const char *buf1, char c, char ***pp, int *count)
    {
        
    }
    
    
    char ** spitString3(const char *buf1, char c, int *count)
    {
    
    }
    */
  • 相关阅读:
    [Luogu5042/UOJ #100][国家集训队互测2015]丢失的题面/ydc的题面
    [51nod1773]A国的贸易
    [GZOI2019&GXOI2019]省选GG记
    [51nod1659]数方块
    [51nod1052]最大M子段和
    [51nod1201]整数划分
    [51nod1084]矩阵取数问题 V2
    [51nod1020]逆序排列
    [BZOJ3000]Big Number
    [BZOJ1684][Usaco2005 Oct]Close Encounter
  • 原文地址:https://www.cnblogs.com/yaozhenhua/p/9416564.html
Copyright © 2011-2022 走看看