Advertisement
Shailrshah

Sorting Strings Alphabetically with 2-D Arrays

Apr 19th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.     char array[10][10], temp[10];
  6.     int i,j,n;
  7.     printf("How many strings?: ");
  8.     scanf("%d",&n);
  9.     printf("Enter %d strings.\n",n);
  10.     for(i=0;i<n;i++)
  11.         scanf("%s",array[i]);
  12.     for(i=0; i<(n-1); i++)
  13.     {
  14.         for(j=0;j<(n-i-1);j++)
  15.         {
  16.             if(strcmp(array[j],array[j+1])>0)
  17.             {
  18.                 strcpy(temp,array[j]);
  19.                 strcpy(array[j],array[j+1]);
  20.                 strcpy(array[j+1],temp);
  21.             }
  22.         }
  23.     }
  24.     printf("Strings after sorting is:-\n");
  25.     for(i=0;i<n;i++)
  26.         printf("%s\n",array[i]);
  27. }
  28. //Output
  29. //How many strings?: 3
  30. //Enter 3 strings.
  31. //Ham
  32. //Eggs
  33. //Beef
  34. //Strings after sorting is:-
  35. //Beef
  36. //Eggs
  37. //Ham
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement