字符串排序

来源:百度知道 编辑:UC知道 时间:2024/05/24 13:06:51
我现在相对我输入的字符串数组排序a[100][100];
例如输入
fge
bcd
abc
dfg
zzz
ttt
输出为
abc
bcd
dfg
fge
ttt
zzz

麻烦请用C语言 写出一个函数使其
排序 谢谢!

#include<stdio.h>
void sort(char s[100][100])
{int i,j,k;
char t[100];
for(i=0;i<3;i++)
{strcpy(t,s[i]);k=0;
for(j=i+1;j<4;j++) if(strcmp(t,s[j])>0)k=j;
if(k>i){strcpy(t,s[i]);strcpy(s[i],s[k]);strcpy(s[k],t);}
}
}
void main()
{char a[4][100]={"fge","bcd","bdd","abc"};
int i;
printf("before sort:\n");
for(i=0;i<4;i++)
printf("%s\n",a[i]);
printf("after sort:\n");
sort(a);
for(i=0;i<4;i++)
printf("%s\n",a[i]);
}

//随手写的,不是最经济的方法。
//100个数组测试太麻烦,可以自己改。