二级指针练习--辅助指针挖字符串练习

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int SplitStr(const char *str, char split, char output[10][30], int *len)
{
int ret = 0;
int itmp = 0;
const char *p = str;
const char *ptmp = str;

if (str == NULL)
{
    ret = -1;
    return ret;
}
do{
    if (p != NULL)
    {
        p = strchr(p, split);
        strncpy(output[itmp], ptmp, p - ptmp);
        itmp++;
        ptmp = p + 1;
        p = p + 1;
    }
    else
        break;
} while (*p != '\0');

*len = itmp;
return  ret;
}

int main5()
{
int ret = 0;
char *str = "11111,222222,3333333,444444,5555555,666666666666,";
char MyAarry[10][30] = {0};
char ctmp = ',';
int len = 0;
int i = 0;

ret = SplitStr(str, ctmp, MyAarry, &len);
if (ret != 0)
{
    printf("func SplitStr error.. \n");
    return ret;
}
for (; i < len; i++)
    printf("%s\n", MyAarry[i]);
printf("%d\n",len);
return 0;
}

int SplitStr2(const char *str, char split, char **output, int *len)
{
int ret = 0;
int itmp = 0;
const char *p = str;
const char *ptmp = str;

if (str == NULL)
{
    ret = -1;
    return ret;
}

do{
    if (p != NULL)
    {
        p = strchr(p, split);
        strncpy(output[itmp], ptmp, p - ptmp);
        output[itmp][p - ptmp] = '\0';
        itmp++;
        ptmp = p + 1;
        p = p + 1;
    }
    else
        break;

} while (*p != '\0');
*len = itmp;
return  ret;
}

int main6()
{
int ret = 0;
char *str = "11111,222222,3333333,444444,5555555,666666666666,";
char **MyAarry = NULL;
char ctmp = ',';
int len = 0;
int i = 0;

MyAarry = (char**)malloc(10 * sizeof(char*));//*myarray[10];
if (MyAarry == NULL)
    return -1;
for (i = 0; i < 10; i++)
{
    MyAarry[i] = (char*)malloc(sizeof(char)*30);
}
ret = SplitStr2(str, ctmp, MyAarry, &len);
if (ret != 0)
{
    printf("func SplitStr error.. \n");
    return ret;
}

for (i=0; i < len; i++)
    printf("%s\n", MyAarry[i]);
printf("%d\n", len);

return 0;
}

void freemem(char **str, int len)
{
int i;
if (str == NULL)
    return;
else
{
    for (i = 0; i < len; i++)
    {
        if (str[i] != NULL)
        {
            free(str[i]);
            str[i] = NULL;
        }
    }
}
free(str);
}

//精确分配内存
char** SplitStr3(const char *str, char split,int *len)
{
int ret = 0;
int itmp = 0;
const char *p = str;
const char *ptmp = str;
char **output;

if (str == NULL)
{
    return NULL;
}

while (p && *p!='\0')
{
    itmp++;
    p = strchr(p,split);
    if (p)
        p = p + 1;
}
*len = itmp;
output = (char**)malloc(sizeof(char*) * itmp);

if (output == NULL)
    return NULL;

p = ptmp = str;
itmp = 0;

do{
    if (p != NULL)
    {
        p = strchr(p, split);
        output[itmp] = (char*)malloc(sizeof(char)*(p-ptmp));
        if (output[itmp] == NULL)
        {
            ret = -1;
            break;
        }
        strncpy(output[itmp], ptmp, p - ptmp);
        output[itmp][p - ptmp] = '\0';
        itmp++;
        ptmp = p + 1;
        p = p + 1;
    }
    else
        break;

} while (*p != '\0');

if (ret!=0)
freemem(output,itmp);

return  output;
}


int main7()
{
int ret = 0;
char *str = "11111,222222,3333333,444444,5555555,666666666666,";
char **MyAarry = NULL;
char ctmp = ',';
int len = 0;
int i = 0;

MyAarry = SplitStr3(str, ctmp, &len);
if (MyAarry == NULL)
{
    printf("func SplitStr error.. \n");
    return -1;
}

for (i = 0; i < len; i++)
    printf("%s\n", MyAarry[i]);
printf("%d\n", len);

return 0;
}

int main()
{
typedef int(MYINT5)[5];//定义数组类型
int i = 0;
MYINT5 array;
for (i = 0; i<5; i++)
{
    array[i] = i;
}

for (i = 0; i<5; i++)
{
    printf("%d ", array[i]);
}

getchar();
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40878579/article/details/80143109