Advertisement
Guest User

String Array Sort

a guest
Apr 3rd, 2010
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define ARR 5
  4. void sort(char *string[], int n);
  5.  
  6. int main()
  7. {
  8.     char string[ARR][64]; int i;
  9.  
  10.     strcpy(string[0], "Judas Priest");
  11.     strcpy(string[1], "Dark Tranquillity");
  12.     strcpy(string[2], "Iron Maiden");
  13.     strcpy(string[3], "Black Sabbath");
  14.     strcpy(string[4], "Dio");
  15.  
  16.     sort(string, ARR);
  17.  
  18.     for (i=0; i < ARR; i++)
  19.     {
  20.         printf("%s\n", string[i]);
  21.     }
  22.  
  23.     return 0;
  24. }
  25.  
  26. void sort(char *string[], int n)
  27. {
  28.     int i, j;
  29.     char tmp[64];
  30.     int change = 1;
  31.  
  32.     for (i=0; i<n-1 && change; ++i)
  33.     {
  34.         change = 0;
  35.         for (j=0; j < n-1-i; ++j)
  36.         {
  37.             if (strcmp(string[j+1], string[j]) < 0)
  38.             {
  39.                 strcpy(tmp, string[j]);
  40.                 strcpy(string[j], string[j+1]);
  41.                 strcpy(string[j+1], tmp);
  42.                 change = 1;
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement