请教:按照输入字符串的第一个单词的长度将字符串排序
各位达人,最近做一个练习题,当中有个题目要求按照输入字符串的第一个单词的长度将整个字符串按照从小到大的顺序来排列,例如,输入了以下10个字符串:
Like your life.
Protecting your dignity.
Your parents love you.
Love your hobby.
The child is cute.
Complete your work.
Loading data.
Long for your things.
Shut down your computer.
system.
排列的结果应为:
The child is cute.
Like your life.
Your parents love you.
Love your hobby.
Long for your things.
Shut down your computer.
Loading data.
system.
Complete your work.
Protecting your dignity.
我按照自己的理解写了如下实现,不过并未达到预期效果。
#include
#include
#include
#define MAX 81
#define SIZE 10
void srt_str_fstwdlen(char *ar[], int n);
int main(void)
{
char str[SIZE][MAX];
int choice, i = 0;
char * ptr[SIZE];
printf("Input %d strings (to quit, press Enter at the beginning of a line):\n", SIZE);
while( i
{
ptr[i] = str[i]; //用ptr[i]拷贝源字符串的指针以便传送给srt_str_fstwdlen()
i++;
}
if(str[0] != NULL)
srt_str_fstwdlen(ptr, i);
puts("Done!");
return 0;
}
void srt_str_fstwdlen(char *ar[], int n)
{
int i , j;
char * temp;
char wd[SIZE][MAX]; //用以收集每个字符串中的第一个单词
for(i = 0; i
for(j = 0; j
{
if(isspace(ar[i][j]))
{
wd[i][j] = '\0'; //将从源字符串拷贝过来的字符形成字符串