Sort an Array of Strings in Alphabetical Order

In C, sorting an array of strings in alphabetical order can be achieved using different methods, such as the strcmp() function with sorting algorithms like Bubble Sort, Quick Sort, or by using standard library functions like qsort(). Sorting strings involves comparing them lexicographically and rearranging them accordingly.

In this tutorial, we will explore multiple approaches to sorting an array of strings in C, with detailed explanations and examples.


Examples of Sorting an Array of Strings

1. Sorting Strings Using Bubble Sort and strcmp()

In this example, we will use the Bubble Sort algorithm along with the strcmp() function to compare and sort strings in alphabetical order.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

void bubbleSort(char arr[][20], int n) {
    char temp[20];
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (strcmp(arr[j], arr[j + 1]) > 0) {
                strcpy(temp, arr[j]);
                strcpy(arr[j], arr[j + 1]);
                strcpy(arr[j + 1], temp);
            }
        }
    }
}

int main() {
    char names[][20] = {"Zebra", "Apple", "Orange", "Banana", "Mango"};
    int size = sizeof(names) / sizeof(names[0]);

    bubbleSort(names, size);

    printf("Sorted Strings:\n");
    for (int i = 0; i < size; i++) {
        printf("%s\n", names[i]);
    }

    return 0;
}

Explanation:

  1. We declare an array names[][20] containing multiple strings.
  2. The function bubbleSort() is used to implement the Bubble Sort algorithm.
  3. The strcmp() function compares two strings lexicographically.
  4. If a string is greater than the next one, we swap them using strcpy().
  5. After sorting, we print the sorted strings using a loop.

Output:

Apple
Banana
Mango
Orange
Zebra

2. Sorting Strings Using qsort() and strcmp()

In this example, we will use the standard library function qsort() with a custom comparison function to sort an array of strings.

main.c

</>
Copy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compareStrings(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}

int main() {
    char *names[] = {"Zebra", "Apple", "Orange", "Banana", "Mango"};
    int size = sizeof(names) / sizeof(names[0]);

    qsort(names, size, sizeof(char *), compareStrings);

    printf("Sorted Strings:\n");
    for (int i = 0; i < size; i++) {
        printf("%s\n", names[i]);
    }

    return 0;
}

Explanation:

  1. We declare an array of string pointers names[] where each string is dynamically allocated.
  2. The compareStrings() function is defined to compare two strings using strcmp().
  3. We use qsort() to sort the array. It takes the array, size, element size, and comparison function.
  4. The strings are sorted alphabetically and printed using a loop.

Output:

Apple
Banana
Mango
Orange
Zebra

Conclusion

In this tutorial, we explored multiple ways to sort an array of strings in alphabetical order:

  1. Bubble Sort with strcmp(): A simple but inefficient method for small arrays.
  2. qsort() with strcmp(): A more efficient way using the C standard library.

Both methods effectively sort strings lexicographically, but qsort() is recommended for larger datasets due to its optimized sorting algorithm.