Tutorialspoint.dev

C Program to Sort an array of names or strings

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.

#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 ");
    for (i = 0; i < n; i++)
        printf("%d: %s ", i, arr[i]);
  
    // Sort the given names
    sort(arr, n);
  
    // Print the sorted names
    printf(" Sorted array is ");
    for (i = 0; i < n; i++)
        printf("%d: %s ", i, arr[i]);
  
    return 0;
}

Output:

Given array is
0: geeksforgeeks 
1: geeksquiz 
2: clanguage 

Sorted array is
0: clanguage 
1: geeksforgeeks 
2: geeksquiz

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter