[求助]对五个字符串进行排序
对"jegged","king","software","today","hello"这个五个字符串进行排序。 搜索更多相关的解决方案:
字符
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_function( const void *a, const void *b);
char list[5][20]={"jegged","king","software","today","hello"};
int main(void)
{
int x;
qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x < 5; x++)
printf("\n%s", list[x]);
getch();
return 0;
}
int sort_function( const void *a, const void *b)
{
return( strcmp(a,b) );
}
----------------解决方案--------------------------------------------------------
终于搞了一个,运行,还行
#include<stdio.h>
main()
{
char *p[]={"jegged","jin","software","joday","jello"},*t;
int i,j,k;
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
{
if(*p[i]>*p[j])
{t=p[i];p[i]=p[j];p[j]=t;}
else if(*p[i]==*p[j])
{
for(k=1;k<5;k++)
{
if(*(p[i]+k)>*(p[j]+k))
{t=p[i];p[i]=p[j];p[j]=t;}
else if(*(p[i]+k)==*(p[j]+k))
continue;
else break;
}
}
}
printf("%s\t%s\t%s\t%s\t%s",p[0],p[1],p[2],p[3],p[4]);
}
[此贴子已经被作者于2006-12-29 17:54:14编辑过]
----------------解决方案--------------------------------------------------------