Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Trier une liste de mot avec ‘qsort()’

 list, list
{filelink=13215}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int string_comp( const void *sp1, const void *sp2 )
 
{
 
  const char * s1 = *(char **)sp1;
  const char * s2 = *(char **)sp2;
  return strcmp( s1, s2 );
}
int main( )
{
  char *mots[ ] = { "mal",   "rien",   "alors", "lui", "programmer",
                    "tutoriel", "apprendre", "écouter",    "lire",  "exemple" };
 int n = sizeof(mots) / sizeof(char *);
 
  qsort( mots, n, sizeof(char *), string_comp);
 
  for ( int j = 0 ; j < 10 ; j++ )
    puts( mots[j] );
 
  return 0;
}

No comments yet.

Leave a Comment