Advertisement
sol4r

Sorting Country Names

Apr 4th, 2023
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void reorder(int n, char *x[]);
  6.  
  7. int main()
  8. {
  9.     char *country_names[] = {"India", "Russia", "Brazil", "China", "South Africa", "Nigeria"};
  10.     int n = sizeof(country_names)/sizeof(country_names[0]);
  11.  
  12.     printf("List of country names before sorting:\n");
  13.     for(int i = 0; i < n; i++)
  14.         printf("%s\n", country_names[i]);
  15.  
  16.     reorder(n, country_names);
  17.  
  18.     printf("\nList of country names after sorting:\n");
  19.     for(int i = 0; i < n; i++)
  20.         printf("%s\n", country_names[i]);
  21.  
  22.     return 0;
  23. }
  24.  
  25. void reorder(int n, char *x[])
  26. {
  27.     int i, j;
  28.     char temp[50];
  29.  
  30.     for(i = 0; i < n-1; i++)
  31.     {
  32.         for(j = i+1; j < n; j++)
  33.         {
  34.             if(strcmp(x[i], x[j]) > 0)
  35.             {
  36.                 strcpy(temp, x[i]);
  37.                 strcpy(x[i], x[j]);
  38.                 strcpy(x[j], temp);
  39.             }
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement