Sort String in Ascending order in C Program

Program-to-Sort-String-in-Ascending-order-in-C

Program Description:

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

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

Ascending order refers to the ascending 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.

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 : Howdy

Output:

String before sort :Howdy(Actual String)

String After sorting :Hdowy(Ascending Order)

In the ASCII table, upper-case characters appear before lower-case characters. As a result, the sorted string will begin with an upper-case letter and progress through lower-case letters.

Prerequisites:

Please read through the following articles to better understand the program

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 Ascending 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 ascending order.

📢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 Ascending order in C Program Explanation:

  1. Start the program by creating a global constant named SIZE with the value of 100. This SIZE constant holds the max size of the string.
  2. Inside the main() function, Declare a string named inputStr with the SIZE size to hold the input string. Also declare a few supporting variables like i, j, etc.
  3. Take the input string from the user using the gets function and store it in the inputStr string.
  4. 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);
  5. 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 greater than the inputStr[j+1]. If it is true, Then Swap the inputStr[j] and inputStr[j+1] variables.
        • 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++
  6. Once the above two for loops are completed, The input string inputStr will be sorted in ascending order.
  7. Print the resultant string on the console.

Program to Sort String in Ascending order in C Language:

Let’s covert the above bubble sort algorithm into the C program to sort the string in ascending order.

Don’t forget the add the string.h header file as we are using the strlen() library function.

Program Output:

Let’s compile and run the program using GCC compiler (Any of your C Compiler).

Compile the Program

$ gcc sort-string-bubble-asc.c

The above command generates an executable file named a.out. ( If you are using it in windows, Then it will create a .exe file)

Run the Program.

Sort-String-in-Ascending-order-in-C-program-output

In the above example, The input string is Howdy and the program sorted the string and presented the Hdowy as the output. As we have specified earlier, The ASCII values of the upper case letters is smaller than the ASCII values of the lowercase letter, That is the reason H is appeared before the lowercase characters in the above output

Let’s look at a few more examples.

As we can see from the above inputs and outputs, The program is generating the desired results.

Exercise:

Rewrite the above program and move the bubble sort algorithm to a function called bubbleSort(). Then call the bubbleSort function from the main() function to sort the string in ascending order.

Related Practice 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...

1 Response

  1. […] C Program to Sort String in Ascending order […]

Leave a Reply