Sort String in Descending order in C Program

Sort-String-in-Descending-order-in-C-Program

Program Description:

Write a Program to Sort String in descending order in C programming language. The program will accept a string from the user and then sorts the given string characters in descending order.

Descending order refers to the descending order of string characters’ ASCII values.

Here is the List of ASCII Characters with ASCII Values

As we have already discussed in our earlier posts, The characters are stored in the form of ASCII Values. So we are going to compare the ASCII Values of each character to sort the string.

📢 This Program is part of the C String Practice Programs series

If we want to sort the string in Alphabetic order, We can simply convert the input string to either lower case or upper case characters, Then apply the bubble sort to sort the string in alphabetical order.

You can learn more about the string lower case to upper case conversion and vice-versa in the following posts.

Let’s look at the excepted input and output of the program.

Example Input and Output:

Input:

Enter a string : CProgramming

Output:

String before sort :CProgramming(Actual String)

String After sorting :rronmmiggaPC(Descending Order)

In the ASCII table, upper-case characters appear before lower-case characters. As a result, the if we sort the string in descending order the lowercase characters will appear first and followed by the uppercase characters.

Prerequisites:

It is recommended to know the C-Strings, C-Arrays, and Functions in C to better understand the following program.

We are going to look at a few methods to sort the string in C language and each program will be followed by a detailed step-by-step explanation and program output. We also provided instructions for compiling and running the program using your compiler.

We are going to use the bubble sort algorithm to sort the characters of the input string. Let’s look at the algorithm of the bubble sort technique.

Bubble Sort Algorithm to Sort String in Descending order:

Bubble sort is a basic sorting algorithm that iterates through the string elements repeatedly, compares adjacent elements, and swaps them if any of the elements are out of order.

The above process is repeated until no swaps are required, at which point the string is considered to be sorted.

Here is the algorithm for the bubble sort

  1. We have a character array/string named inputStr[] string with len length of characters.
  2. for i = 0 to len-1
    • for j = 0 to len-i-1
      • if inputStr[j] < inputStr[j+1]
        • swap(inputStr[j], inputStr[j+1])
  3. Once the above loop is terminated, inputStr string will be sorted in descending order.
  4. print the results

📢The time complexity of the bubble sort it O(n2). So it will be good for small arrays, But not suitable for large arrays.

Sort String in Descending order in C Program Explanation:

  1. Declare a string named inputStr with the SIZE size to hold the input string. The SIZE is a global constant. which holds the max length of the inputStr. Please change this constant value if you want to use large strings.
  2. Take the input string from the user using the gets function and store it in the inputStr string.
  3. In order to use the bubble sort algorithm, We need to know the string length. So get the string length by using the strlen() library function. and save the string length in len variable. – i.e len = strlen(inputStr);
  4. Now, We need to sort the inputStr string using the bubble sort. Bubble sort uses two for loops and swaps the adjacent elements if they are in the wrong order.
    • Start the first for loop, Traverse from i=0 till the i<len-1
      • Nested for loop, Iterate from j=0 till the len-i-1
        • At Each iteration, Check if the inputStr[j] is less than the inputStr[j+1]. If it is true, Then Swap the inputStr[j] and inputStr[j+1] variables. – i.e (inputStr[j] < inputStr[j + 1])
        • We are using a temporary variable temp to swap the elements. We can also use other methods like Swapping without using temporary variables
      • Increment the variable jj++
    • increment the variable ii++
  5. Once the above two for loops are completed, The input string inputStr will be sorted in descending order.
  6. Print the resultant string on the console.

Program to Sort String in Descending order in C Language:

Here is the C Program to Sort the characters of a string in Descending order

As we are using the strlen() library function, We need to include the string.h header file.

Program Output:

Let’s compile and run the program using GCC(Any compiler)

program-to-sort-string-in-descending-order-in-c-output

In the above example, The input string is CProgrammingand the program sorted the string in descending order and presented the rronmmiggaPC as the output.

Here are a few more examples

As we can see from the above inputs and outputs, The program is properly sorting the string in descending order.

Program to Sort String in Descending order using functions in C Language:

Let’s update the above program to use a user-defined function to sort the string. We will move the bubble sort sorting logic to a user-defined function.

By using the functions we can define a function once and can call it as many times as we want. Functions also increase the readability of the program and make it easier to debug issues. Here are the Advantages of functions in programming.

In the above program, we have defined a function named bubbleSort function. This function takes a string as input and sorts the characters of a string in descending order. Here is the prototype of the bubbleSort function – void bubbleSort(char * str)

To sort the input string, Call the bubbleSort() function with the input string from the main() function.

Finally, Display the results on the screen.

Program Output:

Compile and Run the program.

sort-string-in-descending-order-in-c

As we can see from the above output, The program is properly sorting the array.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to Sort String in Descending Order […]

  2. […] C Program to Sort String in Descending Order […]

Leave a Reply