Sorting chars in a String (sorting strings below)
Following from the comment, you can sort a string the very same way you sort any other type in C. You either use the qsort function provided by the standard library, or you use your own function (e.g. bubble sort, heap sort, etc..)
When using qsort your only task is to write a compare function to pass to the qsort function that tells qsort how to sort your data. The basic declaration for qsort is:
void qsort (void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
qsort takes 4 arguments: (1) a pointer to the list/array to be sorted, (2) the number of elements to sort, (3) the type size of each element (e.g. sizeof int), and (4) lastly a pointer to your compare function. Note: your compare function takes 2 generic pointers (void type) as its input.
For sorting characters, you correctly recognize you will sort by the ascii values. Therefore, all your compare function needs to do is return whether of the two characters: (a) one is greater than the other, or (b) they are equal.
An example of the compare function here would be:
int compare_char (const void *a, const void *b) {
if (*(char *)a != *(char *)b)
return *(char *)a - *(char *)b;
return 0;
}
Generally, the only difficult part for new C programers is figuring out how to cast the void type of the arguments to the type you need to compare.
In the compare function, since you are passed a pointer to type void, you need to cast it to your type before you can dereference it to get the value at the pointer address. Here casting the void * to char * is all you need. Then you need to dereference it with '*' before you making the comparison. (for a final cast/dereference of e.g. *(char *)a). Putting it all together in a short example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXS 32
/* simple compare function for qsort, returns
less than 0 if char 'b' is larger than char 'a',
a positiver number if 'a' is larger than 'b', and
zero if they are equal (note: you can return
'b' - 'a' to sort in reverse order.)
*/
int compare_char (const void *a, const void *b) {
if (*(char *)a != *(char *)b)
return *(char *)a - *(char *)b;
return 0;
}
int main (void) {
char word[MAXS] = {0};
char sorted[MAXS] = {0};
printf ("\n Enter a string: "); /* read word from stdin */
if (!scanf ("%31[^\n]%*c", word)) {
printf (" [ctrl+d] received\n\n");
return 1;
}
/* copy word to sorted before sort */
strncpy (sorted, word, sizeof word);
/* sort the chars in sorted[] */
qsort (sorted, strlen (word), sizeof *word, compare_char);
/* print results */
printf ("\n word : %s\n sorted : %s\n\n", word, sorted);
return 0;
}
Compile
$ gcc -Wall -Wextra -Ofast -o bin/str_sort_char str_sort_char.c
Use/Output
$ ./bin/str_sort_char
Enter a string: aghibw
word : aghibw
sorted : abghiw
Sorting The Array of Strings
All the explanation above originally regarding sorting by char's, also applies to sorting an array of strings. This also provides a good look at the way you approach using qsort. Above, in the first example, when we approached using qsort, we had to answer the question: "What type am I sorting?" Above we were sorting each char in a null-terminated array of chars (or string).
How are strings referenced? Through a pointer to the beginning character in the string. So here, instead of sorting each char in an array of chars, we will be sorting strings in an array of strings (or properly an array of pointers to type char *) As far is qsort is concerned, the only difference will be the compare function (and the input...). We need to sort strings, so the compare function must provide a way of comparing strings. In C, the standard string comparison function is strcmp (or strncmp to limit the number of characters compared).
For instance, using strcmp to sort an array of strings, you would use something similar to:
/* string compare function */
int compare_strings (const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}
If you look at the comparison within compare_strings, you are simply returning the result of the strcmp function. Basically, changing only the compare function (and the input), you can then use qsort to sort your array of strings, as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* string compare function */
int compare_strings (const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}
int main (void) {
char *strings[] = {"Baba", "Cece" , "Caca" , "Zaza" , "Lala"};
size_t i;
/* sort strings */
qsort (strings, sizeof strings/sizeof *strings,
sizeof *strings, compare_strings);
/* output sorted arrray of strings */
for (i = 0; i < sizeof strings/sizeof *strings; i++)
printf (" strings[%2zu] : %s\n", i, strings[i]);
return 0;
}
Output
$ ./bin/qsort_strings_static
strings[ 0] : Baba
strings[ 1] : Caca
strings[ 2] : Cece
strings[ 3] : Lala
strings[ 4] : Zaza
char) has a value between 0-127 (basic ascii values) and can be sorted accordingly. Generally a simple sort routing likeqsortcan be used without much effort. For short strings you can use just about any sort routine you choose without much loss in efficiency, but for longer strings, (1000's of chars), quicksort, heapsort or a combination with mergesort provide the best performance. (See Ascii Table Valuesqsortin the most basic possible way".