题目:
有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss,“),
要求写一个函数(接口),输出以下结果要求3:编写正确的测试用例。(30分)。
//头文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/*c语言没有bool型变量,故用int型来代替*/
int divString(const char *buf, char c, char **des, int *count)
{
int i=0;//循环变量
int ncount=0;//二维数组行数
char *p, *q;//指针变量
assert(NULL!=buf);
assert(NULL!=des);
assert(NULL!=count);
p=q=buf;//指针都指向字符串开头
p=strchr(buf,c);//寻找字符c
if (p==NULL)
{
return 0;//没有找到c,返回0
}
while(p!=NULL)
{
memcpy(des[ncount],q,p-q);
des[ncount][p-q]='\0';
p++;
q=p;
p=strchr(p,c);
ncount++;
}
*count=ncount;
return 1;//程序正确运行,返回1
}
//主函数,测试用例
int main()
{
char buf[50]="abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";
int count;
int i=0;
char c=',';
//为二级指针申请内存空间
char **des=(char **)malloc(10*sizeof(char *));
if (des==NULL)
{
return;
}
for (i=0; i<10; i++)
{
des[i]=(char *)malloc(100);
}
if ( divString(buf,c,des,&count))
{
for (i=0; i<count; i++)
{
printf("%s\n", des[i]);
}
}
//释放内存空间
for (i=0; i<10; i++)
{
free(des[i]);
}
if (des)
{
free(des);
}
}
原文地址:http://blog.csdn.net/lsh_2013/article/details/44875787