Posted on 2012-08-19 10:49
hoshelly 閱讀(413)
評(píng)論(0) 編輯 收藏 引用 所屬分類(lèi):
C 、
Programming
//字符串排序,利用qsort函數(shù)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Nmax 1000
#define Mmax 10000
char buf[Mmax]; int M=0;
int compare(const void *i, const void *j)
{
return strcmp(*(char **)i, *(char **)j);
}
int main()
{
int i, N;
char *a[Nmax];
for(N=0; N<Nmax; N++)
{
a[N]= &buf[M];
if(scanf("%s", a[N]) == EOF) break;
M+=strlen(a[N])+1;
}
qsort(a,N,sizeof(char*),compare); //四個(gè)參數(shù)分別代表:待排序的數(shù)組首地址,數(shù)組中待排序的元素?cái)?shù)量,各元素占用的空間,排序函數(shù)(確定排序順序)
for(i=0;i<N;i++)
printf("%s\n",a[i]);
return 0;
}