Advertisement
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
- void swap(char **string_ptr[][MAX_SIZE_STRING], int c);
- 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 option = 0;
- puts("What order would you like to put your strings into?");
- puts("1) Ascending order");
- puts("2) Descending order");
- scanf("%d", &option);
- if (option == 1) {
- 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]) < 0) {
- swap(&string, c);
- }
- c += 1;
- }
- u -= 1;
- }
- }
- else {
- int u = 0, c = 0; //Bubble sorting begins
- u = num_strings;
- while (u > 1) {
- c = 1;
- while (c <= u) {
- if (strcmp(string[c], string[c - 1]) > 0) {
- swap(&string, c);
- }
- c += 1;
- }
- u -= 1;
- }
- }
- index = 0;
- puts("\n\nList:");
- for (index = 0; index < num_strings; index++) {
- puts(string[index]);
- }
- return 0;
- }
- void swap(char **string_ptr[][MAX_SIZE_STRING], int c)
- {
- char *temp = *string_ptr[c];
- *string_ptr[c] = *string_ptr[c - 1];
- *string_ptr[c - 1] = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement