zoukankan      html  css  js  c++  java
  • C语言进阶之路(二)----字符串操作常见模型

    1.while模型

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //求一个字符串中某个子串出现的次数
    int getCout(char *str, char *substr, int *count)
    {
        int rv = 0;
        char *p = str;
        
        int ncout = 0;
        if (str==NULL || substr== NULL ||  count==NULL)
        {
            rv = -1;
            printf("func getCout()check (str==NULL || substr== NULL ||  count==NULL) err:%d 
    " , rv);
            return rv;
        }
        while (*p != ''){
            p = strstr(p, substr);
            if (p == NULL) 
            {
                break;
            }
            else 
            {
                ncout++;
                p = p + strlen(substr);
            }
    
        } ;
        //通过指针把结果传出来
        *count  = ncout;
        return rv;
    }
    
    int main()
    {
        int ret = 0;
        char *p = "abcd1111abcd222abcd3333";
        char *subp = "abcd";
        int ncout = 0;
    
        ret = getCout(p, subp, &ncout);
        if (ret != 0)
        {
            printf("func getCout() err:%d 
    ", ret);
            return ;
        }
        printf("coutn = %d 
    ", ncout);
        return 0;
    }

    2.两头堵模型:两种写法

    //求去掉两边空格之后的字符串长度,指针作为形参传入,将结果赋值给指针指向的内存
    int trimSpaceStr01(char *p, int *mycount)
    {
        int ret = 0;
    
        int ncount = 0;
        int i= 0, j;
        j = strlen(p) - 1;
    
        while (isspace(p[i]) && p[i] != '')
        {
            i++;
        }
    
        while (isspace(p[j]) && j>0)
        {
            j--;
        }
    
        ncount = j - i + 1;
        *mycount = ncount;
        return ret;
    }
    
    //求去掉两边空格之后的字符串,将指针作为形参传入,将结果赋值给形参指向的内存空间
    int trimSpaceStr2(char *p, char *buf)
    {
        int ret = 0;
    
        int ncount = 0;
        int i, j;
        i = 0;
        j = strlen(p) - 1;
    
        while (isspace(p[i]) && p[i] != '')
        {
            i++;
        }
    
        while (isspace(p[j]) && j>0)
        {
            j--;
        }
    
        ncount = j - i + 1;
        //
        strncpy(buf, p + i, ncount);
        buf[ncount] = '';
        return ret;
    }
    
    //这种写法不好
    //不要轻易去改变指针输入特性中in内存块的内存
    int trimSpaceStr2_notgood(char *p)
    {
        int ret = 0;
    
        int ncount = 0;
        int i = 0, j;
        j = strlen(p) - 1;
    
        while (isspace(p[i]) && p[i] != '')
        {
            i++;
        }
    
        while (isspace(p[j]) && j>0)
        {
            j--;
        }
    
        ncount = j - i + 1;
    
        strncpy(p, p + i, ncount);
        p[ncount] = '';
        return ret;
    }
    
    void main()
    {
        {
            char *p = "     abcd     ";
            char buf[1024] = { 0 };
            trimSpaceStr2(p, buf);
            printf("buf = %s
    ", buf);
        }
        
        {
            char *p = "     abcd     ";
            trimSpaceStr2_notgood(p);
            printf("p = %s
    ", p);
        }
    }

    3.字符串反转模型

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //将某个字符串逆置
    void main()
    {
        char p[] = "abcde";
        char c;
        char *p1 = p;
        char *p2 = p + strlen(p) - 1;
    
        while (p1 < p2)
        {
            c = *p1;
            *p1 = *p2;
            *p2 = c;
            ++p1;
            --p2;
        }
    
        printf("p:%s 
    ", p);
    }

    4.两个辅助指针变量挖字符串

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*
    有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";),要求写一个函数(接口),输出以下结果
    1)    以逗号分割字符串,形成二维数组,并把结果传出;
    2)    把二维数组行数运算结果也传出。
    */
    
    int spitString(const char *buf1, char c, char buf[10][30], int *num)
    {
        char *p = NULL;
        char *pTmp = NULL;
        int ncount = 0;
        char myBuf[1024] = { 0 };
    
        //步骤1 初始化条件 pTmp,p都执行检索的开头
        p = buf1;
        pTmp = buf1;
        while (*p != '')
        {
            //步骤2 strstr strchr,会让p后移     在p和pTmp之间有一个差值
            p = strchr(p, c);
            if (p == NULL) //没有找到则跳出来
            {
                break;
            }
            else
            {
                memset(myBuf, 0, sizeof(myBuf));
    
                //挖字符串
                strncpy(myBuf, pTmp, p - pTmp);
                myBuf[p - pTmp] = '';
    
                strcpy(buf[ncount], myBuf);
    
                ncount++;
                //步骤3 让p和pTmp重新初始化,达到检索的条件 
                pTmp = p = p + 1;
            }
    
        } ;
        *num = ncount;
        return 0;
    }
    
    int spitString02(const char *buf1, char c, char buf[10][30], int *num)
    {
        int ret = 0;
        char *p = NULL;
        char *pTmp = NULL;
        int ncount = 0;
        if (buf1 == NULL || num == NULL)
        {
            return -1;
        }
        //步骤1 初始化条件 pTmp,p都执行检索的开头
        p = buf1;
        pTmp = buf1;
        while (*p != '')
        {
            //步骤2 strstr strchr,会让p后移     在p和pTmp之间有一个差值
            p = strchr(p, c);
            if (p == NULL) //没有找到则跳出来
            {
                break;
            }
            else
            {
    
                //挖字符串
                strncpy(buf[ncount], pTmp, p - pTmp);
                buf[ncount][p - pTmp] = '';
    
                ncount++;
    
                //步骤3 让p和pTmp重新初始化,达到检索的条件
                pTmp = p = p + 1;
            }
    
        } ;
        *num = ncount;
        return ret;
    }
    
    void main()
    {
        int ret = 0, i = 0;
        const char *buf1 = "abcdef,acccd,";
        char c = ',';
        char buf[10][30];
        int num = 0;
        ret = spitString02(buf1, c, buf, &num);
        if (ret != 0)
        {
            printf("func spitString() err:%d
    ", ret);
            return ret;
        }
    
        for (i = 0; i<num; i++)
        {
            printf("%s
    ", buf[i]);
        }
    
        system("pause");
    }
  • 相关阅读:
    从ReentrantLock的实现看AQS的原理及应用
    Java 守护线程
    js静态文件编辑器显示操作,但网页显示中文乱码 解决方案
    springmvc 4.3版本集成 Caffeine缓存系统
    chrome浏览器如何设置黑色背景
    电脑型号19 1200 固态SSD
    电脑型号18 1200 固态SSD
    geohash st_distance st_distance_sphere 关系
    Git自动输入账户名密码。明文及SSH私钥2种方式
    一步快速获取 iOS 设备的 UDID
  • 原文地址:https://www.cnblogs.com/crazyzhang/p/5767464.html
Copyright © 2011-2022 走看看