#include#include void sort(char *a[]); void print(char *a[]); int main() { char *a[] ={"ceo","define","basic","abc","empty"}; printf("原來的序列是:\n"); print(a); sort(a); printf("\n排序後的序列是:\n"); print(a); printf("\n"); return 0; } void sort(char *a[]) { int i,j; char *temp; for(i=0;i<4;i++)//注意是i<4 { for(j=0;j<4;j++)//注意是j<4,因為下面要+1和後面的那個字符串比較 { if(strcmp(a[j],a[j+1])>0)//字符串比較:>0表示前面的字符串比後面的大則交換 { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } void print(char *a[]) { int i; for(i=0;i<5;i++) { printf("%s ",a[i]); } }