c语言练习(4)--逗号分割字符串形成二维数组
2017-06-12 22:30
531 查看
/** 作者:一叶扁舟 时间:23:11 2017/6/4 作用: 有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss";),要求写一个函数(接口),输出以下结果 1)以逗号分割字符串,形成二维数组,并把结果传出; 2)把二维数组行数运算结果也传出。 strchr(“aa,aa”,’,’ ); 请自己定义一个接口(函数)。 要求1:能正确表达功能的要求,定义出接口(函数)(30分); 要求2:正确实现接口(函数),并实现功能(40分); 要求3:编写正确的测试用例。(30分)。 **/ #include<stdio.h> #include <stdlib.h> #include <string.h> //获取总共要分几行 void getLine(char *str, char c, int *line){ char *p = str; char *q = str; int num = 0; while (*p != '\0'){ if (*p == c){ num++; } p++; } num = num + 1; *line = num; } char ** getData(char *str, char c, int line){ char *p = str; char *q = str; char **result = (char **)malloc(line*sizeof(char*)); for (int i = 0; i < line; i++){ while (*q != '\0' && *q != c){//没有到字符串的结尾或者遇到分割符c q++; } char *part = (char *)malloc(100 * sizeof(char)); int length = q - p; memcpy(part, p, length);//拷贝数据 part[length] = '\0'; p = q + 1 ; q = q + 1; result[i] = part; } return result; }; void main(){ char *str ="abcdef,acccd,eeee,aaaa,e3eeeee,sssss"; int *line = (int *)malloc(1*sizeof(int)); char c = ','; getLine(str, c, line); printf("%s字符串可以分%d行\n",str,*line); char ** result = getData(str, c, *line); printf("分割的字符串数组数据:\n"); for (int i = 0; i < *line; i++){ printf("%s\n",result[i]); } system("pause"); }
相关文章推荐
- 【字符串】面试题之以逗号分割字符串,形成二维数组
- 以逗号分割字符串,形成二维数组,并把结果传出C/C++
- 有一个字符串符abcdef,acccd,eeee,aaaa,e3eeeee,sssss以逗号分割字符串,形成二维数组,并把结果传出
- C/C++学习之C提高-----非空格的字符串长度、去掉字符串前后空格、字符串反转、键值对字符串、const练习、二级指针做输入/输出的特性、将字符串按逗号分开,并放入二维数组打印出来
- C语言的字符串分割
- 按照指定的字符分割字符串形成字符数组
- C语言中strtok函数进行分割字符串!
- 如何把一段逗号分割的字符串转换成一个数组
- C语言字符串练习1
- Java将一段逗号分割的字符串转换成一个数组
- sql server 中将由逗号“,”分割的一个字符串,转换为一个表,并应用与 in 条件
- 逗号分割的字符串转成表
- [C语言]字符串处理 - 以指定的字符串分割字符串(支持中文字符)
- 逗号分割字符串存储过程
- 逗号分割的字符串存储到临时表中
- 按照指定的字符串分割字符串形成字符数组
- 1.编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容
- 如何在T-SQL中获取逗号分割的字符串数据中的元素?
- 在C语言中分割字符串
- 用C语言来分割字符串