第3题. 动态内存分配,指针数组,模拟函数的传引用调用
从键盘读取正整数n,然后读取n个单词,单词长度假设不会超过20。要求按照单词长度从小到大的顺序,输出单词。
要求:根据n动态创建一个长度为n的指针数组。每个单词存放在动态字符数组中。
(1) 实现函数char ** readWords(int n),用于从键盘读取n个单词,保存在动态申请的字符数组中,字符数组首地址保存在动态申请的指针数组中。返回指针数组首元素的地址。
(2) 实现函数void sort(char * * ptr, int size,int * minlenPtr,int * maxlenPtr),用于对单词按照长度的从小到大顺序进行排序,并将单词的最短长度和最长长度分别写入minlenPtr和maxlenPtr指向的整形变量中。
(3) 实现函数void output(char ** ptr,int size),用于输出单词
(4) 实现函数void freeMemory(char ** ptr,int size),用于释放动态申请的内存,包括指针数组和字符数组的内存空间。
可以调用第1题中实现的mystrlen函数计算字符串长度。
Sample 1:
Please input the number of words:6↙
Please input the words:
Where↙
there↙
is↙
hope↙
a↙
way↙
Min length is 1 and max length is 5
After sort,the words are:
a
is
way
hope
Where
there