Table of contents
1.
Introduction
1.1.
Problem Statement
2.
Approach🤔
2.1.
Implementation 
2.1.1.
Complexity Analysis 
3.
Frequently Asked Questions
3.1.
What is the definition of string C programming?
3.2.
In C, how do you sort a string alphabetically?
3.3.
What is the built-in sort function's time complexity?
3.4.
In the C programming language, which loop is the fastest?
4.
Conclusion
Last Updated: Feb 3, 2025
Easy

C Program to arrange the string in alphabetical order

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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;
}
You can also try this code with Online C Compiler
Run Code

Output : 

Complexity Analysis 

Time Complexity: O(n^2), where n is the length of the entered string.

Frequently Asked Questions

What is the definition of string C programming?

A string in C programming is a sequence of characters that ends with the null symbol \0.  Example -  char c[] = "c string"; By default, the compiler appends a null character 0 to the end of a sequence of characters surrounded in double quotation marks.

In C, how do you sort a string alphabetically?

The user must submit a number of names, which must then be sorted in alphabetical order using the strcpy() function. A string is a collection of characters (or an array of characters). 

What is the built-in sort function's time complexity?

As most languages implement timsort, which is a hybrid of merge sort and insertion sort, the built-in sort function has a nlogn time complexity.

In the C programming language, which loop is the fastest?

In C programming, the Do-While loop is the fastest loop.

Conclusion

In this article, we have discussed three examples of C Programming to arrange the string in alphabetical order. We hope that this article has helped you enhance your knowledge regarding sorting the strings. If you would like to learn more, check out our articles on  rewind-function-in-cc-program-to-concatenate-two-stringsShort int in C Programming,  c-program-to-copy-first-string-into-second-stringsorting-array-except-elements-in-a-subarray and many more.

Recommended Readings:

 

Recommended Problems - 

 

Live masterclass