字符串操作常見模型(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");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章