字符串操作常见模型(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 \n" , rv);
        return rv;
    }
    while (*p != '\0'){
        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 \n", ret);
        return ;
    }
    printf("coutn = %d \n", 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] != '\0')
    {
        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] != '\0')
    {
        i++;
    }

    while (isspace(p[j]) && j>0)
    {
        j--;
    }

    ncount = j - i + 1;
    //
    strncpy(buf, p + i, ncount);
    buf[ncount] = '\0';
    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] != '\0')
    {
        i++;
    }

    while (isspace(p[j]) && j>0)
    {
        j--;
    }

    ncount = j - i + 1;

    strncpy(p, p + i, ncount);
    p[ncount] = '\0';
    return ret;
}

void main()
{
    {
        char *p = "     abcd     ";
        char buf[1024] = { 0 };
        trimSpaceStr2(p, buf);
        printf("buf = %s\n", buf);
    }
    
    {
        char *p = "     abcd     ";
        trimSpaceStr2_notgood(p);
        printf("p = %s\n", 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 \n", 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 != '\0')
    {
        //步骤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] = '\0';

            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 != '\0')
    {
        //步骤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] = '\0';

            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\n", ret);
        return ret;
    }

    for (i = 0; i<num; i++)
    {
        printf("%s\n", buf[i]);
    }

    system("pause");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章