给定一个所有字符都大小写的字符串数组,编写一个C函数按字母顺序对它们进行排序。
这个想法是在C语言中使用qsort()并编写一个比较函数,该函数使用strcmp()比较两个字符串。
#include
#include
#include
// 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;
}
输出:
Given array is
0: geeksforgeeks
1: geeksquiz
2: clanguage
Sorted array is
0: clanguage
1: geeksforgeeks
2: geeksquiz