C program to sort characters of a string as per their ASCII values

C program to Sort characters in a string as per their ASCII values :

In this C programming tutorial, we will learn how to sort all the characters of a user input string as per their ASCII values. ASCII stands for American Standard code for Information Interchange. It is a standard that assigns a numerical value to each character.

There are total 256 integer values starting from 0 to 255 to represent all characters. For example, the ASCII value of A is 65 and the ASCII value of a is 97. Similarly, the ASCII value of < is 60. Any character you can type has an ASCII value.

Check this website to know all ASCII values

Our problem is to sort all the characters in a string as per their ASCII values in ascending order. For example, the string ‘aA’ will become ‘Aa’ because the ASCII value of ‘A’ is 65 and ‘a’ is 97.

Algorithm :

  1. To solve this problem, we will scan the characters of the given string one by one and store the count of each character in an array. We need to create one integer array of size 256 as we have 256 ASCII characters.
  2. Initialize one array of size 256 and assign all the elements of the array to 0.
  3. Read the characters of the string one by one from start to end.
  4. For each character, increment the value of the array item by 1 at the ASCII index. For example, if the current character is ‘A’, increment the value on the ‘65’th position of the array. If one ‘A’ is found, the final value of this index position will become 1, if 10 ‘A’s are found in the string, it will be 10.
  5. So, after all the characters of the string are read, the array will hold the count of each character in its specific position.
  6. We need to iterate the full array again from start to end to find and print the characters. If array[97] = 2, we need to print the character ‘a’ two times. Since we are iterating from index 0 to 255 of the array, it will print the characters of the string in increasing order of their ASCII values.

C program to sort the characters of a string as per the ASCII values:

The following program reads a string as an input from the user and prints the characters sorted in increasing order as per their ASCII values:

#include <stdio.h>

int main()
{
    // 1
    int countArray[256];
    char inputString[100];
    int i, j;

    // 2
    printf("Enter a string : ");
    fgets(inputString, 100, stdin);

    // 3
    for (i = 0; i < 256; i++)
    {
        countArray[i] = 0;
    }

    // 4
    for (i = 0; inputString[i]; i++)
    {
        countArray[inputString[i]]++;
    }

    // 5
    printf("\nAfter sorting : ");

    // 5
    for (i = 0; i < 256; i++)
    {
        for (j = 0; j < countArray[i]; j++)
        {
            printf("%c", i);
        }
    }
}

Explanation :

The commented numbers in the above program denote the step numbers below:

  1. Create one integer array countArray of size 256. This will hold the count of each character of a string. Create one more character array inputString to store the user input array.
  2. Ask the user to enter a string. Read it and assign it to the variable inputString.
  3. Set the value of all the elements of countArrray to 0.
  4. Iterate over the characters of the given string one by one. Increment the value of countArray for the ASCII value position by 1. The statement countArray[inputString[i]]++ is used to increment the value at index position inputString[i] which is equal to the ASCII value of the character at position i of the string inputString.
  5. Print the final sorted string.
  6. We are using one for loop to iterate over the items of the countArray one by one. If the value at any index is greater than 0, we need to print out the character denoted by that specific position for n number of times if n is the value for that position.

C sort characters of a string as per ASCII values

Sample Output :

Enter a string : aldsjldsfalkfdsa

After sorting :
aaadddffjklllsss

Enter a string : helloworld

After sorting :
dehllloorw

Download it on Github

You might also like: