C Program to sort String characters in alphabetically ascending order
|In this C Programming example, we will implement the program to sort characters in a string in ascending alphabetical order and print the output on the console.
1. Sort characters in a string – Selection Sort
To sort characters in a string in ascending alphabetical order, we will first iterate over the characters and then compare two characters at a time. If in case the characters are not found in the correct order then we will swap the characters using a temporary variable and repeat the process until we reach null character(or no more characters are left for comparison) and then print the output on the console.
This process of sorting is also known as Selection sort. Please refer to Selection Sorting for details.
Example: Input: helloworld Output: dehllloorw
Helpful topics to understand this program better are-
- Data Types in C
- Console Input / Output in C
- Arrays, Functions and Recursive functions in C
- For and While Loop in C
2. C Program to sort characters in a string in ascending alphabetical order
Let’s discuss the execution(kind of pseudocode) for the program to sort characters in a string in ascending alphabetical order in C.
- Initially, the program will prompt the user to enter a string.
- After the string is entered,
fgets(input, 100, stdin)
holds the string and then passes the string toascendingOrderString()
function. - Next, the
for (i = 0; i < stringLength-1; i++) and for (j = i+1; j < stringLength; j++)
loops keeps on iterating over the characters and compare each character with every other character. - If the characters don’t match the order, then swap and continue the process until all the characters are in order and print the output on the console.
Let us now implement the above execution of the program to sort characters in a string in ascending alphabetical order in C.
Note: This program works on the ASCII value of the character so all A-Z are smaller than a-z.
#include <stdio.h> #include <string.h> char input[100]; void ascendingOrderString() { int i, j; char temp; int stringLength = strlen(input); for (i = 0; i < stringLength - 1; i++) { for (j = i + 1; j < stringLength; j++) { if (input[i] > input[j]) { temp = input[i]; input[i] = input[j]; input[j] = temp; } } } } int main() { printf("\n\t Enter the string : "); fgets(input, 100, stdin); ascendingOrderString(); puts(input); return 0; }
Output Enter the string : Codingeek Cdeegikno
Note: In the above program the characters must be entered under the declared range char input[100].
To sort the characters in descending order we just have to flip one condition and everything else remains the same.
Change input[i] > input[j] =to=> input[i] < input[j]
3. Conclusion
In this C Programming example, we have discussed how to sort characters in a string in ascending alphabetical order in C.
Helpful Links
Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.
Also for the example C programs please refer to C Programming Examples.
All examples are hosted on Github.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?