Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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: Mar 27, 2024
Easy

C Program to arrange the string in alphabetical order

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

Also see: C Static Function and  Tribonacci Series.

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

You can also read about dynamic array in c and Tribonacci Series

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.

Must Read what is storage class in c

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 Problems - 

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Previous article
C Program to Count Uppercase, Lowercase, and Special Characters in the String
Next article
C program to merge two sorted array in descending order
Live masterclass