Introduction
In this blog we will discuss the approach to arrange the string in alphabetical order. Before jumping on to the approach to the problem, let us first understand the problem.

Problem Statement
This article discusses a program that sorts a string in alphabetical order. The main aim is to sort a string into alphabetical order and display the results as output.

Consider the following strings as an example.
Input: whale Output: aehlw Input: night Output: ghint |
Approach🤔
To solve this problem we will iterate over characters in a string and thereafter compare the two characters at a time to sort them in ascending alphabetical order. If the characters are not discovered in the correct order, we will swap them using a temporary variable and continue the procedure until we hit null character (or there are not any more characters for comparison), at which point we will report the output to the console.
Let's look at how the program to sort characters in a string in ascending alphabetical order in C is executed.
Algorithm
Step 1: The program will ask the user to enter a string .
Step 2: fgets(ch, 50, stdin) holds the string once it is entered and then sends it to the ascendingOrder() function.
Step 3: Following that, the for(i = 0; i < SLength-1; i++) and for (j = i+1; j < SLength; j++) loops iterate over the characters, comparing each one to the others.
Step 4: If the characters don't match the order, swap them.
Repeat the process until all of the characters are in the correct order, then display the result to the console.
Implementation
#include <stdio.h>
#include <string.h>
char ch[50];
void ascendingOrder() {
int i, j;
char tem;
int SLength = strlen(ch);
for (i = 0; i < SLength - 1; i++) {
for (j = i + 1; j < SLength; j++) {
if (ch[i] > ch[j]) {
tem = ch[i];
ch[i] = ch[j];
ch[j] = tem;
}
}
}
}
int main() {
printf("\n Enter a string that you want to be arranged in alphabetical order : ");
fgets(ch, 50, stdin);
ascendingOrder();
puts(ch);
return 0;
}
Output :
Complexity Analysis
Time Complexity: O(n^2), where n is the length of the entered string.