Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define MAX_NUM_STRINGS 20
- #define MAX_SIZE_STRING 30
- int main(void)
- {
- char *string[MAX_NUM_STRINGS][MAX_SIZE_STRING]; //Array of pointers to strings
- int index = 0, num_strings = 0, current_string = 1;
- printf("How many strings would you like to have? (Max of 20): ");
- scanf("%d", &num_strings); //User enters # of strings they would like to have (MAX_SIZE_STRING = 20)
- for (index = 0; index < num_strings; index++) {
- printf("Enter string #%d", current_string); //User enters each string
- scanf("%s", string[index]);
- current_string++;
- }
- index = 0;
- puts("List:"); //Print off the unorganized list of strings
- for (index = 0; index < num_strings; index++) {
- puts(string[index]);
- }
- int u = 0, c = 0; //Bubble sorting begins
- u = num_strings - 1;
- while (u > 1) {
- c = 1;
- while (c < u) {
- if (strcmp(string[c], string[c - 1])) { //string[c-1] comes right before string[c]. If the string
- char *temp = string[c - 1]; //located in string[c] comes earlier in the alphabet than the
- *string[c - 1] = string[c]; //string in string[c-1], the pointers pointing to each string will need
- *string[c] = temp; //to swap. Lines 37-39 are supposed to swap the pointers but
- //it isn't working.
- }
- c += 1;
- }
- u -= 1;
- }
- index = 0;
- puts("List:");
- for (index = 0; index < num_strings; index++) {
- puts(string[index]);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment