#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");
}
有一个字符串符abcdef,acccd,eeee,aaaa,e3eeeee,sssss以逗号分割字符串,形成二维数组,并把结果传出
最新推荐文章于 2021-11-11 11:54:15 发布