C Program to Sort an array of names or strings Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 19 Likes Like Report Given an array of strings in which all characters are of the same case, write a C function to sort them alphabetically. The idea is to use qsort() in C and write a comparison function that uses strcmp() to compare two strings. C #include <stdio.h> #include <stdlib.h> #include <string.h> // Defining comparator function as per the requirement static int myCompare(const void* a, const void* b) { // setting up rules for comparison return strcmp(*(const char**)a, *(const char**)b); } // Function to sort the array void sort(const char* arr[], int n) { // calling qsort function to sort the array // with the help of Comparator qsort(arr, n, sizeof(const char*), myCompare); } int main() { // Get the array of names to be sorted const char* arr[] = { "geeksforgeeks", "geeksquiz", "clanguage" }; int n = sizeof(arr) / sizeof(arr[0]); int i; // Print the given names printf("Given array is\n"); for (i = 0; i < n; i++) printf("%d: %s \n", i, arr[i]); // Sort the given names sort(arr, n); // Print the sorted names printf("\nSorted array is\n"); for (i = 0; i < n; i++) printf("%d: %s \n", i, arr[i]); return 0; } OutputGiven array is 0: geeksforgeeks 1: geeksquiz 2: clanguage Sorted array is 0: clanguage 1: geeksforgeeks 2: geeksquiz Time Complexity: O(nlogn)Auxiliary Space: O(1) Comment K kartik Follow 19 Improve K kartik Follow 19 Improve Article Tags : Strings Sorting DSA Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like