#include "string.h"
#include "stdio.h"
int Scan_Charnum(char *str,int *char_count,char c)
{
int num = 0;
char *temp_str = str;
char *temp;
if (str == NULL || char_count == NULL || c == NULL)
return -1;
while (1)
{
temp_str = strchr(temp_str, c);
if (temp_str != NULL)
{
temp_str = temp_str + 1;
num++;
}
else
{
break;
}
}
*char_count = num;
return 1;
}
int Scan_Char_Eachrow(char *str,char c)
{
}
int Assign_Mem(char *str,char ***p,int num,char c)
{
char **temp;
int i = 0;
int row_num;
char *temp_str;
char *temp2;
int ret = 1;
temp_str = str;
temp = (char **)malloc(sizeof(char *)*num);
if (temp == NULL)
{
ret = -1;
goto END;
}
memset(temp, 0, num*sizeof(char *));
for (i = 0; i < num ; i++)
{
temp2 = strchr(temp_str, c);
if (temp2 == NULL)
{
temp2 = temp_str + strlen(temp_str);
}
row_num = temp2 - temp_str;
temp[i] = (char *)malloc(sizeof(char)*(row_num+1));
if (temp[i] == NULL)
{
ret = -1;
goto END;
}
memcpy(temp[i], temp_str, row_num);
memset(temp[i] + row_num, '\0', 1);
temp2++;
temp_str = temp2;
}
END:
if (ret == -1)
{
Free_Mem(&temp, num);
}
else
{
*p = temp;
}
return ret;
}
int Print_Str(char **p, int num)
{
int i = 0;
for (i = 0; i < num; i++)
{
printf("%s\n",p[i] );
}
}
int Free_Mem(char ***p, int num)
{
char **temp;
temp = *p;
int i = 0;
if (p == NULL)
{
return 1;
}
for (i = 0; i < num; i++)
{
if (p[i] != NULL)
{
free(p[i]);
p[i] = NULL;
}
}
if (temp != NULL)
{
temp = NULL;
free(temp);
}
}
void main()
{
char *p = "aa,cc,dd,343";
int num = 0;
int row_num = 0;
char c;
char **te=NULL;
int flag_mem;
c = ',';
Scan_Charnum(p, &num, c);
row_num = num + 1;
Assign_Mem(p, &te, row_num, c);
Print_Str(te, row_num);
Free_Mem(te, row_num);
}