C program to sort each word from a given string in descending order

In this article, we will learn about how to sort or arrange each word from any given string (by the user at run-time) in descending order.

In this program, each word is sorted character-wise. For example, if the given string by the user is "this is codescracker," Here we have a total of three words ("this," "is," and "codescracker") present inside the string. Therefore, after sorting each word, the string will become "tsih si srrokeedccca."

In this way, the following program will sort each and every word from the given string in descending order (alphabetical-wise).

#include<stdio.h>
#include<conio.h>
int main()
{
    char str[100], chTemp;
    int i, j, len;
    printf("Enter any string: ");
    gets(str);
    len = strlen(str);
    for(i=0; i<len; i++)
    {
        for(j=0; j<(len-1); j++)
        {
            if(str[j]>=65 && str[j]<=90)
            {
                if(str[j+1]>=65 && str[j+1]<=90)
                {
                    if(str[j]<str[j+1])
                    {
                        chTemp = str[j];
                        str[j] = str[j+1];
                        str[j+1] = chTemp;
                    }
                }
            }
            if(str[j]>=97 && str[j]<=122)
            {
                if(str[j+1]>=97 && str[j+1]<=122)
                {
                    if(str[j]<str[j+1])
                    {
                        chTemp = str[j];
                        str[j] = str[j+1];
                        str[j+1] = chTemp;
                    }
                }
            }
        }
    }
    printf("\nSame string with each word in descending order:\n%s", str);
    getch();
    return 0;
}

The program was written in the Code::Blocks IDE; therefore, after a successful build and run, here is its sample run:

c sort each word in descending order

Now supply any string, say "how are you," and press the ENTER key to see the same string with its words sorted in descending order:

sort each word in descending order c

Here is a list of some of the main steps used in the above program:

  • Receive any string using the gets() function.
  • Now find the length of the given string using the strlen() function of string.h library.
  • Store the length of a given string inside any variable, say len.
  • Create a for loop that starts from 0 and ends at one less than the length of the string to compare each and every character.
  • Inside the for loop, create another for loop to run or repeat comparing the string's every character.
  • Inside the second for loop, confirm that both characters are alphabetical or not, compare and reverse only when the first character is less than the second character, the second character is less than the third character, and so on.
  • Let's suppose that the user supplied a string, say "codescracker," then c is compared with o first, as c is less than o, therefore the reverse operation gets performed, and now the string will become "ocdescracker" after first checking and reversing. Now again the second character, that is, c, is compared with the third character, d, and here again c is less than d, so again the reverse operation gets performed, and d goes to c's position and c goes to d's position, and so on.
  • In this way, all the characters of the string get sorted in descending order.
  • Here, we have used two loops to do comparison and reversal (if the previous is less than the next character) twice from start to end.
  • Finally, print the value of the string on the output screen, which will be the same string but arranged in descending order.

C Quiz


« Previous Program Next Program »


Follow/Like Us on Facebook


Subscribe Us on YouTube