说明:原题目中的const要删除,否则过不了编译。因为const了就不能排序了…… #include #include "string.h"int GetWords(char *sentence, char *words[]);void SortStrings(char *strs[], int count);//const int main(int argc,char *argv[]){ char str[200]; int nWords = 0; char *words[20]; int i; printf("input a string: "); gets(str); nWords = GetWords(str,words); SortStrings(words, nWords); puts("output:"); for(i=0;i0) k=j; if(k-i) p=strs[i],strs[i]=strs[k],strs[k]=p; } /******end******/} 执行结果如下:
int read_word(char **c)
{
char k[21];
int i;
for (i = 0; i < 150; i++)
{
gets(k);
if (k[0] == '#')
return i;
*(c + i) = (char *)malloc(21 * sizeof(char));
strcpy(*(c + i), k);
}
return i;
}
void sort(char **c, int n)
{
char temp[21];
int i,j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - 1 - i; j++)
if (strlen(*(c + j)) > strlen(*(c + j + 1)))
{
strcpy(temp, *(c + j));
strcpy(*(c + j), *(c + j + 1));
strcpy(*(c + j + 1), temp);
}
}
void show(char **c, int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%s ", *(c + i));
}
}