limegreen

Untitled

Aug 27th, 2016
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_NUM_STRINGS 20
  5. #define MAX_SIZE_STRING 30
  6.  
  7. int main(void)
  8. {
  9. char *string[MAX_NUM_STRINGS][MAX_SIZE_STRING]; //Array of pointers to strings
  10. int index = 0, num_strings = 0, current_string = 1;
  11.  
  12. printf("How many strings would you like to have? (Max of 20): ");
  13. scanf("%d", &num_strings); //User enters # of strings they would like to have (MAX_SIZE_STRING = 20)
  14.  
  15. for (index = 0; index < num_strings; index++) {
  16. printf("Enter string #%d", current_string); //User enters each string
  17. scanf("%s", string[index]);
  18. current_string++;
  19. }
  20.  
  21. index = 0;
  22. puts("List:"); //Print off the unorganized list of strings
  23. for (index = 0; index < num_strings; index++) {
  24. puts(string[index]);
  25. }
  26.  
  27.  
  28.  
  29. int u = 0, c = 0; //Bubble sorting begins
  30.  
  31. u = num_strings - 1;
  32.  
  33. while (u > 1) {
  34. c = 1;
  35. while (c < u) {
  36. if (strcmp(string[c], string[c - 1])) { //string[c-1] comes right before string[c]. If the string
  37. char *temp = string[c - 1]; //located in string[c] comes earlier in the alphabet than the
  38. *string[c - 1] = string[c]; //string in string[c-1], the pointers pointing to each string will need
  39. *string[c] = temp; //to swap. Lines 37-39 are supposed to swap the pointers but
  40. //it isn't working.
  41. }
  42. c += 1;
  43. }
  44. u -= 1;
  45. }
  46.  
  47.  
  48.  
  49. index = 0;
  50. puts("List:");
  51. for (index = 0; index < num_strings; index++) {
  52. puts(string[index]);
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment