How to sort a string using qsort in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main() {
    char str[] = "cprogramming";

    qsort(str, strlen(str), sizeof(char), compare_function);
    
    puts(str);
    
    return 0;
}



/*
run:

acggimmnoprr

*/

 



answered Jan 19, 2024 by avibootz

Related questions

1 answer 110 views
110 views asked Nov 11, 2021 by avibootz
1 answer 189 views
189 views asked Dec 4, 2019 by avibootz
1 answer 291 views
1 answer 143 views
1 answer 118 views
...