有一个字符串符abcdef,acccd,eeee,aaaa,e3eeeee,sssss以逗号分割字符串,形成二维数组,并把结果传出

#define _CRT_SECURE_NO_WARNINGS
#include"stdio.h"
#include"string.h"
#include"stdlib.h"

/*
有一个字符串符合以下特征(”abcdef, acccd, eeee, aaaa, e3eeeee, sssss";),要求写一个函数(接口),输出以下结果
以逗号分割字符串,形成二维数组,并把结果传出;
把二维数组行数运算结果也传出。
strchr(“aa, aa”, ’, ’);
请自己定义一个接口(函数)。
要求1:能正确表达功能的要求,定义出接口(函数)(30分);
要求2:正确实现接口(函数),并实现功能(40分);
要求3:编写正确的测试用例。(30分)。
*/

int getTheStrBuf(char *str, char c, char **buf){
	int ret = 1;
	if (str == NULL || buf == NULL){
		ret = 0;
		printf("func getTheStrBuf() error:%d str == NULL || buf == NULL", ret);
		return ret;
	}
	int count = 0;
	char *p = str;
	char *ptemp = p;
	while (p = strchr(p, c)){
		while (ptemp < p){
			memcpy(*(buf + count), ptemp, p - ptemp);//注意可能出现错误的地点
			*(*(buf + count) + (p - ptemp)) = '\0';
			count++;
			p++;
			ptemp = p;

		}
	}
	if (ptemp != p){
		strcpy(*(buf + count), ptemp);
	}
	return ret;
}
void main()
{
	char str[] = "abcdef,acccd,eeeee,aaaaa,e3eeeee,sssss";
	char **buf;
	char c;
	int i = 1;
	char *p;
	p = str;
	if (str != NULL){
		while (*p != '\0'){
			if (*p == ','){
				i++;
			}
			p++;
		}
	}
	else{
		printf("The str is NULL");
	}
	buf = (char **)malloc(sizeof(char)*i);
	for (int z = 0; z < i; z++){
		*(buf + z) = (char *)malloc(sizeof(char)*strlen(str));
	}
	printf("请输入断点字符:");
	scanf("%c", &c);
	getTheStrBuf(str, c, buf);
	for (int j = 0; j < i; j++){
		printf("%s\n", *(buf + j));
	}
	system("pause");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值