C Program to sort each Word from given String in Ascending Order

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

In this program, Each word is sorted character wise, For example, if the given string by user will be this is codescracker. Here we have total of three words (this, is and codescracker) present inside the string. Therefore after sorting each word, the string will become hist is acccdeekorrs. As you can see that, all the three words are sorted or arranged in ascending order.

#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 ascending order:\n%s", str);
    getch();
    return 0;
}

The program was written under Code::Blocks IDE, therefore after successful build and run, here is the sample run:

sort each word in ascending order c

Supply any string say how are you and press ENTER key to see the same string with its each word sorted in ascending order as shown in the second snapshot of the sample run given here:

c sort each word in ascending order

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

  • Receive any string using gets() function
  • Now find length of the given string using strlen() function of string.h library
  • Store the length of given string inside any variable say len
  • Create a for loop that starts from 0 to one less than the length of the string to compare its each and every character
  • Inside the for loop, create another for loop to run or repeat comparing the string's each and every character
  • Inside the second for loop, be confirm that both the character is an alphabet or not, to compare and reverse only when previous character is greater than the next character
  • Let's suppose if user supplies the string say codescracker, then c is compared with o first, as it is already arranged, then again o is compared with next character of the string that is d
  • Now we have to apply the reverse operation to place d before o (because o is greater than d) and continue to check for the next character, that is o with e and so on
  • In this way, all the characters of the string gets sorted
  • Here, we have used two for loop to do comparing and reversing (if previous is greater than the next character) two times from start to beginning
  • Finally print the value of string on output screen which will be the same string but arranged in ascending order

C Online Test


« Previous Program Next Program »