C Program to Sort a String of Characters Last Updated : 05 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program.The most straightforward method to sort a string of characters is by using the qsort() function. Let's take a look at an example: C #include <stdio.h> #include <stdlib.h> #include <string.h> // Comparator function for qsort int comp(const void *a, const void *b) { return (*(char *)a - *(char *)b); } int main() { char s[] = "adfecb"; // Sort the string using qsort qsort(s, strlen(s), sizeof(char), comp); printf("%s", s); return 0; } OutputabcdefExplanation: The qsort() function needs a comparator that guides it how to compare two values of the given dataset. In this case, it tells how two characters should be compared and the order in which they should be arranged.C language only provides the built-in method for the quick sort algorithm. If you need any other sorting algorithm, you will have to implement them yourself as shown:Using Bubble SortBubble Sort is a simple comparison-based sorting algorithm which repeatedly compares adjacent characters and swaps them if they are in the wrong order. C #include <stdio.h> // Implementation of bubble sort void bubbleSort(char *s) { int n = 0; while (s[n] != '\0') n++; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (s[j] > s[j + 1]) { char temp = s[j]; s[j] = s[j + 1]; s[j + 1] = temp; } } } } int main() { char s[] = "adfecb"; // Sort the string using Bubble Sort bubbleSort(s); printf("%s", s); return 0; } OutputabcdefUsing Selection SortSelection Sort is another comparison-based sorting algorithm that works by finding the smallest character from the unsorted portion of the string and swapping it with the first unsorted character. This process is repeated until the entire string is sorted. C #include <stdio.h> // Implementation of selection sort void selSort(char *s) { int n = 0; while (s[n] != '\0') n++; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (s[j] < s[minIndex]) { minIndex = j; } } char temp = s[minIndex]; s[minIndex] = s[i]; s[i] = temp; } } int main() { char s[] = "adfecb"; // Sort the string using Selection Sort selSort(s); printf("%s", s); return 0; } Outputabcdef Comment More infoAdvertise with us Next Article C Program to Sort a String of Characters A abhishekcpp Follow Improve Article Tags : C Programs C Language C-String C Searching Sorting C-DSA +1 More Similar Reads C Program to Swap Adjacent Characters of a String In this article, we will learn how to swap adjacent characters of a string in C. To swap all adjacent characters in a string, the string must have an even number of characters. If the number of characters is odd, the last character remains unswapped since it has no adjacent element.The most straight 3 min read How to Append a Character to a String in C? In this article, we will learn how to append a character to a string using the C program.The most straightforward method to append a character to a string is by using a loop traverse to the end of the string and append the new character manually.C#include <stdio.h> void addChar(char *s, char c 3 min read C Program to Compare Two Strings Using Pointers In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers.To compare two strings using pointers, increment the pointers to traverse through each character of the strings whil 2 min read Array of Pointers to Strings in C In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings 2 min read How to Reverse a String in C? In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu 2 min read Like