C Program to Sort a String | Sorting

Introduction

In this post, I am going to write a c program to sort a string. We know that sorting is nothing but an arrangement in which alphabets or numbers are arranged in ascending or descending order.

Also Read: Program To Reverse a String in C using Pointer

In this program, we will ask the user to enter a string and then we will display sorted string. See the following expected output.

Expected Output

c program to sort a string

As you can see in the above output, string is sorted in ascending order. Blank space is less than a therefore it comes before a.

You can sort this string in descending order also. You just need to change the conditions of if statement.

Now, let us see the actual c program.

C Program to Sort a String in Ascending Order

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char str[100];
    int i,j,temp;
    printf("Enter a string\n");
    gets(str);
    printf("Original String: %s",str);
    for(i=0;i<strlen(str);i++)
    {
        for(j=i+1;j<strlen(str);j++)
        {
            if(str[i]>str[j])
            {
                temp = str[i];
                str[i]=str[j];
                str[j]=temp;
            }
        }
    }
    printf("\nSorted String: %s",str);
    return 0;
}

Explanation

Now, I am going to explain some part of this program.

Variable Declarations

In this program, I have declared four variables i.e. str, i, j and temp.

The variable str is nothing but a character array. We are going to store the input string.

I am using two for loops here. For this, we need two loop variables and they are i and j.

In the loop, I am comparing one character is greater than other character and if this is true, then we are exchanging the positions of both these characters. For this, I need one temporary variable i.e. temp.

Swapping Part

There are two loops. In the second loop, there is a if statement. In this, we are comparing the first character with the second character. If that character is greater than the second character, then this program will exchange this first character and second character. If not, then the first character will be compared with the third character and so on.

When the j loop is over that means we have got our first smallest character.

Next, we are going to compare second character with third character and so on.

In this way, we will get the sorted string.

I hope you have understood this program. Now, I am going to write the same program for sorting a string in descending order.

C Program to Sort a String in Descending Order

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char str[100];
    int i,j,temp;
    printf("Enter a string\n");
    gets(str);
    printf("Original String: %s",str);
    for(i=0;i<strlen(str);i++)
    {
        for(j=i+1;j<strlen(str);j++)
        {
            if(str[i]<str[j])
            {
                temp = str[i];
                str[i]=str[j];
                str[j]=temp;
            }
        }
    }
    printf("\nSorted String: %s",str);
    return 0;
}

Now, you can easily observe the if condition. Instead of greater than operator, I have used less than operator.

If you have any difficulty for understanding this program, kindly let me know. Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment