C Online Compiler
Example: String Sort with qsort() in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// String Sort with qsort() #include <stdio.h> #include <string.h> // For strlen #include <stdlib.h> // For qsort // Step 1: Custom comparison function for characters int compareChars(const void *a, const void *b) { return (*(char*)a - *(char*)b); } int main() { char str[] = "programming"; int length = strlen(str); // Step 2: Call qsort function // Parameters: // - str: Pointer to the array to be sorted // - length: Number of elements in the array // - sizeof(char): Size of each element // - compareChars: Pointer to the comparison function qsort(str, length, sizeof(char), compareChars); // Step 3: Print the sorted string printf("Original string: programming\n"); printf("Sorted string: %s\n", str); return 0; }
Output
Clear
ADVERTISEMENTS