Sort the Characters of a string in C – Sort String in Ascending and Descending order

Spread the love
Sort-the-Characters-of-a-string-in-C-Program

Program Description:

Write a Program to Sort the Characters of a string in C programming language. The program will accept a string from the user and sorts the characters of strings in Ascending order and Descending order of character ASCII Values.

We are going to look at different methods to sort the characters in a string in C. 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 favorite compiler.

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

Before going to look at the program. Let’s understand the program requirements by looking at an example input and output of the program.

Example Input and Output:

Input:

Enter a string : Howdy, How are you?

Output:

String before sorting :Howdy, How are you?(Actual string)

,?HHadeoooruwwyy

The program sorts the characters based on the ASCII Values of the Characters. You can check the ASCII Values of all characters in C in the following post.

Prerequisites:

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

We will look at the Two methods to Sort the characters.

  1. Sorting the string Ascending Order of Characters
  2. Sorting the string in Descending Order of Characters

We are going to use the C standard library function qsort() to sort the characters in the string.

Syntax of Qsort() function:

  • string is the string to be sorted
  • num is the length of the string
  • size is the size of the data type i.e character. so it will be equal to sizeof(char)
  • compare_function is a function pointer, which points to the comparison function.

Method 1: Sort the Characters in String in Ascending Order using Qsort function in C:

Sort String in Ascending Order Program Explanation:

Start the program by declaring a string to store the input string(i.e inputStr). The size of the string is equal to the SIZE constant. which is equal to 100.

Prompt the user to provide the input string and update the inputStr character array/string. We are using the gets function to read the string from the user.

Now, To sort the string character in Ascending order, We need to define a compare function.

This charCompareAsc function compares the values pointed by the pointer variables a and b.

The function takes two void pointers a and b as arguments. These pointers are cast to char pointers and dereferenced in order to access the characters they point to.

The function returns the difference between the two characters, *(char *)a - *(char *)b, which will be

  • A Negative value if *a is less than *b. We are checking the ASCII Values of the character here.
  • A Positive value if *a is greater than *b.
  • Zero if *a is equal to *b

Let’s use this compareCharAsc function with the qsort() function to sort the characters of the string in ascending order.

Program to Sort the string characters in Ascending order:

Here is the program to sort the string in Ascending order using qsort.

Output:

Compile and Run the program using your favorite compiler

sort-characters-of-string-in-ascending-order-in-c-program-output

As we can see from the above output, The program sorted the characters of the given string in Ascending order of the ASCII Values. Note that the ASCII Values of the upper case letters is smaller than the lower case letter. That is the reason the upper case W came first in the output.

Let’s look at a few more examples.

As excepted, The program is properly sorting the characters in ascending order.

Method 2: Sort the Characters in String in Descending Order using Qsort function in C Language:

Sort String in Descending Order Program Explanation:

Similar to the above program, Declare the input string ( inputStr) and Also take the user input and update the inputStr string.

Now, To sort the string character in Descending order, We need to define a compare function.

This charCompareDsc function compares the values pointed by the pointer variables a and b.

The function takes two void pointers a and b as arguments. These pointers are cast to char pointers and dereferenced in order to access the characters they point to.

The function returns the difference between the two characters, *(char *)b - *(char *)a, which will be

  • A Positive value if *b is greater than *a. We are checking the ASCII values of the characters.
  • A Negative value if *b is less than *a.
  • Zero if *b is equal to *a

Let’s use this compareCharDsc function with the qsort() function to sort the characters of the string in descending order.

Program to Sort the string characters in Descending order:

Here is the program to sort the string in Descending order using qsort.

Program Output:

Compile and Run the program

sort-characters-of-string-in-descending-order-in-c-program-output

As we can see from the above output, The program is properly sorting the string characters in Descending order.

For example, The input string micheal characters are sorted in descending order and the resultant string is mliheca.

Related Programs:

Venkat

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. […] Sort String in Ascending and Descending order using Qsort() […]

Leave a Reply