Advertisement
limegreen

Untitled

Aug 27th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 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. void swap(char **string_ptr[][MAX_SIZE_STRING], int c);
  8.  
  9. int main(void)
  10. {
  11. char *string[MAX_NUM_STRINGS][MAX_SIZE_STRING]; //Array of pointers to strings
  12. int index = 0, num_strings = 0, current_string = 1;
  13.  
  14. printf("How many strings would you like to have? (Max of 20): ");
  15. scanf("%d", &num_strings); //User enters # of strings they would like to have (MAX_SIZE_STRING = 20)
  16.  
  17. for (index = 0; index < num_strings; index++) {
  18. printf("Enter string #%d", current_string); //User enters each string
  19. scanf("%s", string[index]);
  20. current_string++;
  21. }
  22.  
  23. index = 0;
  24. puts("List:"); //Print off the unorganized list of strings
  25. for (index = 0; index < num_strings; index++) {
  26. puts(string[index]);
  27. }
  28.  
  29. int option = 0;
  30.  
  31. puts("What order would you like to put your strings into?");
  32. puts("1) Ascending order");
  33. puts("2) Descending order");
  34. scanf("%d", &option);
  35.  
  36. if (option == 1) {
  37.  
  38. int u = 0, c = 0; //Bubble sorting begins
  39.  
  40. u = num_strings - 1;
  41.  
  42. while (u > 1) {
  43. c = 1;
  44. while (c < u) {
  45. if (strcmp(string[c], string[c - 1]) < 0) {
  46. swap(&string, c);
  47. }
  48. c += 1;
  49. }
  50. u -= 1;
  51. }
  52. }
  53. else {
  54. int u = 0, c = 0; //Bubble sorting begins
  55.  
  56. u = num_strings;
  57.  
  58. while (u > 1) {
  59. c = 1;
  60. while (c <= u) {
  61. if (strcmp(string[c], string[c - 1]) > 0) {
  62. swap(&string, c);
  63. }
  64. c += 1;
  65. }
  66. u -= 1;
  67. }
  68. }
  69.  
  70.  
  71.  
  72. index = 0;
  73. puts("\n\nList:");
  74. for (index = 0; index < num_strings; index++) {
  75. puts(string[index]);
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. return 0;
  84. }
  85.  
  86.  
  87. void swap(char **string_ptr[][MAX_SIZE_STRING], int c)
  88. {
  89.  
  90. char *temp = *string_ptr[c];
  91. *string_ptr[c] = *string_ptr[c - 1];
  92. *string_ptr[c - 1] = temp;
  93.  
  94.  
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement