写两个函数(API),输出以下结果
第一个API
1)以逗号分隔字符串,形成二位数组,并把结果传输
2)把二维数组行数运算结果也传出
第二个API
1)以逗号分隔字符串,形成一个二级指针
2)把一共拆分多少行字符串个数传出
要求:
1.能正确表达功能的要求,定义出接口。
2.正确实现接口和功能
3.编写正确的测试用例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int spitString(char *str,char ch,char array[][30],int *count)
{
char *p = str;
char *q = p;
int temp_count = 0;
if (str == NULL || array == NULL || count == NULL )
{
fprintf(stderr, "str == NULL || array == NULL || count == NULL\n");
return -1;
}
//在母串中找到一个字符,找到了返回第一个字符的地址,失败返回NULL
while((p=strchr(p,ch))!=NULL)
{
strncpy(array[temp_count][30],q,p-q);
array[temp_count][p-q] = '\0';
temp_count++;
p++;
q = p;
if (*p == '\0')break;
}
if (*q != '\0')
{
int len = (str + strlen(str))-q;//求偏移量
strncpy(array[temp_count],q,len);
array[temp_count][len] = '\0';
temp_count++;
}
*count = temp_count;
return 0;
}
int main(int argc, char const *argv[])
{
char *str = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";
char array[10][30];
int retn = 0;
int count = 0;
retn = spitString(str,',',array,&count);
if (retn <0)
{
fprintf(stderr, "spitString er\n");
}
for (int i = 0; i < count; ++i){
printf("array[%d]:%s\n",i,array[i]);
}
return 0;
}
==============================第二种==================================
int spitString(char *str,char ch,char ***array_p,int *count)
{//修改外面的二级指针,需要三级指针
char *p = str;
char *q = p;
int temp_count = 0;
char **array = NULL
if (str == NULL || array_p == NULL || count == NULL ){
fprintf(stderr, "str == NULL || array == NULL || count == NULL\n");
return -1;
}
//1.求出字符串中拆分的个数
while((p=strchr(p,ch))!= NULL){
temp_count++;
p++;
q = p;
if (*q == '\0')break;
}
if (*q != '\0')temp_count++;
//2.根据个数开辟指针数组,在堆上
array = (char **)malloc(sizeof(char*) *temp_count);//这里不懂为什么二级指针是这样!
if (array == NULL){
fprintf(stderr, "malloc char **array error\n");
return -1;
}
memset(array,0,sizeof(char*)*temp_count);
//3.拆分字符串,为每一个指针开辟堆空间,拷贝字符串
p = str;
q = p;
temp_count = 0;
int str_len = 0;
while((p = strchr(p,ch))!= NULL)//找到了
{
str_len = p - q;
array[temp_count] = (char*)malloc(sizeof(char*)*(str_len+1));
if (array[temp_count] == NULL){
fprintf(stderr, "malloc char array[%d] error\n",temp_count);
return -1;
}
memset(array[temp_count],0,sizeof(char*)*(str_len+1));
strncpy(array[temp_count],q,str_len);
array[temp_count][str_len] = '\0';
temp_count++;
p++;
q = p;
if (*p == '\0')
{
break;
}
}
if (*q != '\0')
{
str_len = (str+strlen(str))-q;
array[temp_count] = (char*)malloc(sizeof(char*)*(str_len+1));
if (array[temp_count] == NULL){
fprintf(stderr, "malloc char array[%d] error\n",temp_count);
return -1;
}
strncpy(array[temp_count],q,str_len);
array[temp_count][str_len] = '\0';
temp_count++;
}
if (array != NULL)
{
*array_p = array;
*count = temp_count;
}
return 0;
}
void free_mem(char ***array_p,int count)
{
char **array = *array_p;
int i = 0;
if (array_p == NULL) return ;
if (array != NULL)
{
for ( i = 0; i <count ; ++i)
{
if (array[i] != NULL)
{
free(array[i]);
array[i] = NULL
}
}
free(array);
*array_p = NULL;//置空,防止野指针
}
}
int main(int argc, char const *argv[])
{
char *str = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";
char **array = NULL;
int count = 0;
int retn = 0;
retn = spitString(str,',',&array,&count);
for (int i = 0; i < count; ++i)
{
printf("array[%d]:%s\n",,i,array[i]);
}
free(&array,count);
return 0;
}
网友评论