C下按照指定格式截取子串

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

//  有一个字符串符合以下特征(“abcdef,acccd,eeee,aaaa,e3eeee,ssss,”)
//
//	写两个函数(API),输出以下结果
//	第一个API(第二种内存模型: 二维数组)
//	1)以逗号分隔字符串,形成二维数组,并把结果传出
//	2)把二维数组行数运算结果也传出
//	int spitString(const char *str, char c, char buf[10][30]/*in*/, int *count);
//
//	第二个API(第三种内存模型:动态生成二维内存)
//	1)以逗号分隔字符串,形成一个二级指针。
//	2)把一共拆分多少行字符串个数传出
//	int spitString2(const char *str, char c, char **myp /*in*/, int *count);

int spitString(const char *str, char c, char buf[10][30], int *count)
{
	if (str == NULL || count == NULL)
	{
		return -1;
	}
	// str = "abcdef,acccd,eeee,aaaa,e3eeee,ssss"
	const char *start = str;
	char * p = NULL;
	int i = 0;

	do 
	{
		p = strchr(start, c);
		if (p != NULL)
		{
			int len = p - start;
			strncpy(buf[i], start, len);
			//结束符
			buf[i][len] = 0;

			i++;

			//重新设置起点位置
			start = p + 1;
		}
		else
		{
			//printf("start = %s\n", start);
			strcpy(buf[i], start);
			i++;

			break;
		}

	} while (*start != 0);

	if (i == 0)
	{
		return -2;
	}


	*count = i;

	return 0;
}

int main01(void)
{
	const char *p = "abcdef,acccd,eeee,aaaa,e3eeee,ssss";
	char buf[10][30] = { 0 };
	int n = 0;
	int i = 0;
	int ret = 0;

	ret = spitString(p, ',', buf, &n);
	if (ret != 0)
	{
		printf("spitString err:%d\n", ret);
		return ret;
	}

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

	printf("\n");
	return 0;
}

//

char **getMem(int n)
{
	char **buf = NULL; //char *buf[n]
	buf = (char **)malloc(n * sizeof(char *)); //char *
	if (buf == NULL)
	{
		return NULL;
	}

	int i = 0;
	for (i = 0; i < n; i++)
	{
		buf[i] = (char *)malloc(30);
		memset(buf[i], 0, 30);
	}
	
	return buf;
}

int getMem2(char ***tmp, int n)
{
	char **buf = NULL; //char *buf[n]
	buf = (char **)malloc(n * sizeof(char *)); //char *
	if (buf == NULL)
	{
		return -1;
	}

	int i = 0;
	for (i = 0; i < n; i++)
	{
		buf[i] = (char *)malloc(30);
		memset(buf[i], 0, 30);
	}

	//间接赋值是指针存在最大意义
	*tmp = buf;

	return 0;
}

int spitString2(const char *str, char c, char **buf, int *count)
{
	if (str == NULL || count == NULL)
	{
		return -1;
	}
	// str = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,"
	const char *start = str;
	char * p = NULL;
	int i = 0;

	do
	{
		p = strchr(start, c);
		if (p != NULL)
		{
			int len = p - start;
			strncpy(buf[i], start, len);
			//结束符
			buf[i][len] = 0;

			i++;

			//重新设置起点位置
			start = p + 1;

		}
		else
		{
			strcpy(buf[i], start);
			i++;

			break;
		}

	} while (*start != 0);

	if (i == 0)
	{
		return -2;
	}


	*count = i;

	return 0;
}

void freeBuf3(char **buf, int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		free(buf[i]);
		buf[i] = NULL;
	}

	if (buf != NULL)
	{
		free(buf);
		buf = NULL;
	}
}

void freeBuf4(char ***tmp, int n)
{
	char **buf = *tmp;

	int i = 0;
	for (i = 0; i < n; i++)
	{
		free(buf[i]);
		buf[i] = NULL;
	}

	if (buf != NULL)
	{
		free(buf);
		buf = NULL;
	}

	*tmp = NULL;
}

int main(void)
{

	const char *p = "abcdef,acccd,eeee,aaaa,e3eeee,ssss";
	char **buf = NULL;
	int n = 0;
	int i = 0;
	int ret = 0;

#if 0
	buf = getMem(6);
	if (buf == NULL)
	{
		return -1;
	}
#endif
	ret = getMem2(&buf, 6);
	if (ret != 0)
	{
		return ret;
	}

	ret = spitString2(p, ',', buf, &n);
	if (ret != 0)
	{
		printf("spitString err:%d\n", ret);

		return ret;
	}

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


// 	freeBuf3(buf, n);
// 	buf = NULL;

	freeBuf4(&buf, n);
	if(buf != NULL)
	{
		free(buf);
		buf = NULL;
	}

#if 0
	for (i = 0; i < n; i++)
	{
		free(buf[i]);
		buf[i] = NULL;
	}

	if (buf != NULL)
	{
		free(buf);
		buf = NULL;
	}
#endif

	return 0;
}

测试结果

在这里插入图片描述

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

//分清楚赋值指针变量 和 操作逻辑之间的关系
int spitString(const char *buf1, char c, char buf2[10][30], int *count)
{
	const char *p=NULL, *pTmp = NULL;
	int	tmpcount = 0;
    
    if(buf1 == NULL || count == NULL)
        return -1;

	p    = buf1;
	pTmp = buf1;

	do 
	{
		//检索符合条件的位置 p后移  形成差值 挖字符串
		p = strchr(p, c);
		if (p != NULL)
		{
			if (p-pTmp > 0)
			{
				strncpy(buf2[tmpcount], pTmp,  p-pTmp);
				buf2[tmpcount][p-pTmp]  = '\0';  //把第一行数据变成 C风格字符串
				tmpcount ++;
				//重新 让p和ptmp达到下一次检索的条件
				pTmp = p = p + 1;
			}
		}
		else
		{
			break;
		}
	} while (*p!='\0');
	
	*count = tmpcount;
    
	return 0;
}

int main(int argc, const char * argv[])
{
	int ret = 0, i = 0;
	char *p1 = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";
	char cTem= ',';
	int nCount;

	char myArray[10][30];
    memset(myArray, 0, sizeof(myArray));

    ret = spitString(p1, cTem, myArray, &nCount);
	if (ret != 0)
	{
		printf("fucn spitString() err: %d \n", ret);
		return ret;
	}

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

测试结果

在这里插入图片描述

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

//主调函数分配内存           ======= 二维数组
int spitString(const char *buf1, char c, char buf2[][30], int *count)
{
	const char *p=NULL, *pTmp = NULL;
	int	tmpcount = 0;

	p    = buf1;
	pTmp = buf1;
	
	do 
	{
		//检索符合条件的位置 p后移  形成差值 挖字符串
		p = strchr(p, c);
		if (p != NULL)
		{
			if (p-pTmp > 0)
			{
				strncpy(buf2[tmpcount], pTmp,  p-pTmp);
				buf2[tmpcount][p-pTmp]  = '\0';  //把第一行数据变成 C风格字符串
				tmpcount ++;
				//重新让p和ptmp达到下一次检索的条件
				pTmp = p = p + 1;
			}
		}
		else
		{
			break;
		}
	} while (*p!='\0');
	
	*count = tmpcount;
	return 0;
}

int main(int argc, const char * argv[])
{
	int ret = 0, i = 0;
	char *p1 = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";
	char cTem= ',';
	int nCount;

	char myArray[10][30];

	ret = spitString(p1, cTem, myArray, &nCount);
	if (ret != 0)
	{
		printf("fucn spitString() err: %d \n", ret);
		return ret;
	}

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

	return 0;
}


测试结果

在这里插入图片描述

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

//主调函数分配内存 ===== 在堆区开辟空间
int spitString(const char *buf1, char c, char **myp /*in*/, int *count)
{
	const char *p=NULL, *pTmp = NULL;
	int	tmpcount = 0;

	p = buf1;
	pTmp = buf1;

	do 
	{
		//检索符合条件的位置 p后移  形成差值 挖字符串
		p = strchr(p, c);
		if (p != NULL)
		{
			if (p-pTmp > 0)
			{
				strncpy(myp[tmpcount], pTmp,  p-pTmp);
				myp[tmpcount][p-pTmp]  = '\0';  //把第一行数据变成 C风格字符串
				tmpcount ++;
				//重新 让p和ptmp达到下一次检索的条件
				pTmp = p = p + 1;
			}
		}
		else
		{
			break;
		}
	} while (*p!='\0');
	
	*count = tmpcount;

	return 0;
}

int main(int argc, const char * argv[])
{
	int ret = 0, i = 0;
	char *p1 = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";
	char cTem= ',';
	int nCount;

	char **p = NULL;  //char buf[10][30]
	p = (char **)malloc(10 * sizeof(char *)); // char * array[10]
	if (p == NULL)
	{
		return -1;
	}
	
	for (i=0; i<10; i++)
	{
		p[i] = (char *)malloc(30 * sizeof(char)); 
	}

	ret = spitString(p1, cTem, p, &nCount);
	if (ret != 0)
	{
		printf("fucn spitString() err: %d \n", ret);
		return ret;
	}

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

	//释放内存
	for (i=0; i<10; i++)
	{
		free(p[i]);
		p[i] = NULL;
	}
	
	free(p);
	p = NULL;
	
	return 0;
}


测试结果

在这里插入图片描述

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

char ** spitString(const char *buf1, char c, int *count)
{
	const char *p=NULL, *pTmp = NULL;
	int	       tmpcount = 0;
	char    **myp = NULL;

	p    = buf1;
	pTmp = buf1;


	//第一遍求出count
	do 
	{
		//检索符合条件的位置 p后移  形成差值 挖字符串
		p = strchr(p, c);
		if (p != NULL)
		{
			if (p-pTmp > 0)
			{
				tmpcount ++;
				//重新 让p和ptmp达到下一次检索的条件
				pTmp = p = p + 1;
			}
		}
		else
		{
			break;
		}
	} while (*p!='\0');

	*count = tmpcount;

	//根据多少行 精确的分配内存
	myp = (char **)malloc(tmpcount * sizeof(char *) );
	if (myp == NULL)
	{
		return NULL;
	}

	tmpcount = 0;
	//p和ptmp初始化
	p = buf1;
	pTmp = buf1;

	do 
	{
		//检索符合条件的位置 p后移  形成差值 挖字符串
		p = strchr(p, c);
		if (p != NULL)
		{
			if (p-pTmp > 0)
			{
				int len = p - pTmp + 1;
				myp[tmpcount] = (char *)malloc(len * sizeof(char));
				if (myp[tmpcount] == NULL)
				{
					return NULL;
				}
				strncpy(myp[tmpcount], pTmp,  p-pTmp);
				myp[tmpcount][p-pTmp]  = '\0';  //把第一行数据变成 C风格字符串
				tmpcount ++;
				//重新 让p和ptmp达到下一次检索的条件
				pTmp = p = p + 1;
			}
		}
		else
		{
			break;
		}
	} while (*p!='\0');
	
	return myp;
}

void FreeMem2(char **myp, int count)
{
	int i =0; 
	if (myp == NULL)
	{
		return;
	}
	for (i=0; i<count; i++)
	{
		if (myp[i] != NULL)
		{
			free(myp[i]);
		}
	}
	if (myp != NULL)
	{
		free(myp);
	}
}

void FreeMem3(char ***p, int count)
{
	int i =0; 
	char **myp = NULL;
	if (p == NULL)
	{
		return;
	}
	myp = *p;

	if (myp == NULL) 
	{
		return ;
	}

	for (i=0; i<count; i++)
	{
		if (myp[i] != NULL)
		{
			free(myp[i]);
		}
	}
	if (myp != NULL)
	{
		free(myp);
	}
	*p = NULL; //把实参二级指针 ,修改成NULL
}

int  spitString4(const char *buf1, char c, char ***myp3, int *count)
{
	int ret = 0;
	const char *p=NULL, *pTmp = NULL;
	int	tmpcount = 0;
	char **myp = NULL;

	pTmp = buf1;
	p = buf1;

	//第一遍求出count
	do 
	{
		//检索符合条件的位置 p后移  形成差值 挖字符串
		p = strchr(p, c);
		if (p != NULL)
		{
			if (p-pTmp > 0)
			{
				tmpcount ++;
				//重新 让p和ptmp达到下一次检索的条件
				pTmp = p = p + 1;
			}
		}
		else
		{
			break;
		}
	} while (*p!='\0');

	*count = tmpcount;

	//根据多少行 精确的分配内存
	myp = (char **)malloc(tmpcount * sizeof(char *) );
	if (myp == NULL)
	{
		ret = -1;
		printf("func spitString4() err:%d  (tmpcount * sizeof(char *) )", ret);
		goto END;
		//return -1;
	}
	memset(myp, 0, tmpcount * sizeof(char *));

	tmpcount = 0;
	p    = buf1;
	pTmp = buf1;

	do 
	{
		//检索符合条件的位置 p后移  形成差值 挖字符串
		p = strchr(p, c);
		if (p != NULL)
		{
			if (p-pTmp > 0)
			{
				int len = p - pTmp + 1;
				myp[tmpcount] = (char *)malloc(len * sizeof(char));
				if (myp[tmpcount] == NULL)
				{
					ret = -2;
					printf("func spitString4() err:%d  malloc(len * sizeof(char) )", ret);
					goto END;
				}
				strncpy(myp[tmpcount], pTmp,  p-pTmp);
				myp[tmpcount][p-pTmp]  = '\0';  //把第一行数据变成 C风格字符串
				tmpcount ++;
				//重新 让p和ptmp达到下一次检索的条件
				pTmp = p = p + 1;
			}
		}
		else
		{
			break;
		}
	} while (*p!='\0');

END:	
	if (ret != 0) //失败
	{
		//FreeMem2(NULL, *count ); 
		//myp= NULL;
		FreeMem3(&myp, *count);
	}
	else
	{
		*myp3 = myp;  //成功
	}	
	return ret;
}

#if 0
//多级指针 避免野指针 
int main(int argc, const char * argv[])
{
	int ret = 0, i = 0;
	char *p1 = "abcdef,acccd,";
	char cTem= ',';
	int nCount;

	char **p = NULL;  //char buf[10][30]


	p= spitString(p1, cTem, &nCount);
	if (p == NULL)
	{
		printf("fucn spitString() err: %d \n", ret);
		return ret;
	}

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

	//释放内存
	for (i=0; i<nCount; i++)
	{
		free(p[i]);
		p[i] = NULL;
	}
	
	free(p);
	p[i] = NULL;
	
	return 0;
}

#else

int main(int argc, const char * argv[])
{
	int ret = 0, i = 0;
	char *p1 = "abcdef,111222,444444,";
	char cTem= ',';
	int nCount;

	char **p = NULL;  //char buf[10][30]


	ret = spitString4(p1, cTem, &p,  &nCount);
	if (ret != 0)
	{
		printf("fucn spitString() err: %d \n", ret);
		return ret;
	}

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


	//释放内存
	for (i=0; i<nCount; i++)
	{
		free(p[i]);
	}
	free(p);

	return 0;
}

#endif

测试结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值