Latest :

C Program : Sorting a String in Alphabetical Order – 2 Ways

Sorting a String in Alphabetical Order C Program – If we are given a problem then, the first step is to understand the problem statement fully and also make sure that, if any constraints are mentioned then they are taken note of.

The next step is, to determine the necessary inputs to solve the given problem. Following this, we also decide on the output to expect at the end. Later, we come up with the logical approach to tackle the given problem and arrive at our desired output with the help of inputs taken.

The given problem here is, to write a C program to sort a given string in alphabetical order. To do so, we require a string whose alphabets are to be sorted as input for the code. After all the operations, the output we desire is a string whose alphabets are sorted.

To read the input string (c) at runtime, we use the gets() function. This is a predefined function in C which reads the string input of one line and stores it in a string pointer or character array.

This continues to read the input until it encounters either a newline or end-of-file. The syntax for reading a string using gets() is – gets(string *s).

char c[200];

printf(“Enter a string: “);

gets(c);

To sort it, we’ll first require the length (n) of the input string . For this, we have a predefined function strlen() from the “string.h” library. Then we iterate from the first index (0) to the last index (n-1) and for each index (i), we again iterate from first index (j=0) to the (n-i-1)th index.

In each case, we check whether the c[j] value is greater than its next next i.e., c[j+1] value. If this condition satisfies then, we have larger alphabet before smaller alphabet hence, we need to swap them.

For swapping we can use a temporary variable (t). We can store the c[j] value initially in t and then, store the c[j+1] value in c[j]. Then, we can store the value of t (i.e., initially c[j] value) to c[j+1]. By the end of this, c[j] and c[j+1] would be swapped.

n=strlen(c);

for (i = 0; i < n-1; i++) {

for (j = 0; j < n-i-1; j++) {

if (c[j] > c[j+1]) {

t = c[j];

c[j] = c[j+1];

c[j+1] = t;

}

}

}

At the completion of first inner loop (j), the last alphabet is the largest.

Then, we proceed with next iteration of the outer loop and at that time the last but one index will have the second largest alphabet. This way by the end of all the iterations of the outer loop and after exiting both the for loops, the end value in the string (c) would be all the alphabets in sorted order.

This is our desired resultant output. To display this on the console screen, we can make use of predefined output function printf() and display the string.

printf(“String after sorting : %s”,c);

Sort a String in Java alphabetically in C

Using Function

In the above code we have seen the logic to sort a given string in alphabetical order. But there are two primary disadvantages in the code because, the entire code is within main method only. One is that, if the same logic is needed elsewhere in the code we can’t reuse it and finding a part of code would be a very hideous task as the code is not quite readable.

To avoid these, we can split the code on the basis of the logic and place them in separate function blocks. This way, if a logic is needed somewhere then instead of rewriting we can just call the function and pass the necessary parameters thereby, making the code reusable.

Also, when everything is placed in different function block this way then, code looks a lot cleaner and readable and, we can find a part of code by directly going to its corresponding function.

To implement this for the given problem, we split the code into two parts. The main method will handle all the input/output operations while a separate user-defined function (sort) will handle the logic behind the sorting mechanism.

Main method reads input string (c) using gets() function and then passes this as parameter to the user-defined function (sort). This function has the same logic as discussed above to sort it in alphabetical  order. The same parameter by the end of this of this function has the sorted string which is then displayed on the output console screen using printf() function.

x

Check Also

C Program To Find Maximum Between Three Numbers | C Programs

C Program to find maximum between three numbers –¬† In this article, we will detail ...