1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   |  
void tri(char **tab, int(*cmp)(void *, void *))
{
  char **p1,**p2;
 
  for(p1=tab; *p1!=NULL; p1++)
    for(p2=p1+1; *p2!= NULL; p2++)
      if((*cmp)(*p1,*p2) > 0)
	{
	  char buf[BUFSIZ];
 
	  strcpy(buf,*p1);
 
	  strcpy(*p1,*p2);
 
	  strcpy(*p2,buf);
                }
}
 
 
void print(FILE *f, char **tab)
{
for( ; *tab!=NULL; ++tab)
fprintf(f,"%s\n",*tab);
}
 
 
 
int main(int argc, char *argv[])
{
  char **t = malloc(sizeof(char *)*argc);
  if(t == NULL)
    {
      fprintf(stderr,"erreur \n");
      return EXIT_FAILURE;
    }
 
  unsigned int l,i; 
 
  l = (unsigned int) argc-1U;
 
  for(i = 0; i<l; i++)
    {
      t[i] = malloc(strlen(argv[i+1])+1);
      if(t[i] == NULL)
	{
	  fprintf(stderr,"erreur \n");
	  return EXIT_FAILURE;
	}
 
      strcpy(t[i],argv[i+1]);
 
    }
 
  t[l] = NULL;
  print(stdout,t);
  tri(t,&strcmp));
  print(stdout,t);
 
  return EXIT_SUCCESS;
} | 
Partager