Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // CTest.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4.  
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #define NUM_WORDS 10
  10. #define LENGTH 20
  11.  
  12. void sort(char words[][LENGTH], int num)
  13. {
  14.  
  15. int i;
  16.  
  17. int j;
  18.  
  19. char temp[LENGTH];
  20.  
  21. for ( i = 0 ; i < num - 1 ; i++ )
  22. {
  23.  
  24. for ( j = i + 1 ; j < num ; j++ )
  25. {
  26.  
  27.  
  28. if ( strcmp(words[i], words[j]) > 0 )
  29. {
  30.  
  31. strcpy(temp, words[i]);
  32.  
  33. strcpy(words[i], words[j]);
  34.  
  35. strcpy(words[j], temp);
  36. }
  37.  
  38. }
  39. }
  40. }
  41.  
  42. int main(void)
  43. {
  44. char array[NUM_WORDS][LENGTH];
  45.  
  46. int i;
  47. int j;
  48. int num = 0;
  49.  
  50. for ( i = 0 ; i < NUM_WORDS ; i++ )
  51. {
  52.  
  53. printf("Enter a word: ");
  54.  
  55. scanf("%s", array[i]);
  56.  
  57. if ( array[i][0] == '0' )
  58. break;
  59. num++;
  60.  
  61. }
  62.  
  63. sort(array, num);
  64.  
  65. printf("\nYour sorted list is: ");
  66.  
  67. for ( j = 0 ; j < num ; j++ )
  68. {
  69.  
  70. printf("%s ", array[j]);
  71. }
  72.  
  73. printf("\n");
  74.  
  75. return 0;
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement