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.
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 :
- 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.
- Initialize one array of size 256 and assign all the elements of the array to 0.
- Read the characters of the string one by one from start to end.
- 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.
- So, after all the characters of the string are read, the array will hold the count of each character in its specific position.
- 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:
- Create one integer array
countArray
of size 256. This will hold the count of each character of a string. Create one more character arrayinputString
to store the user input array. - Ask the user to enter a string. Read it and assign it to the variable
inputString
. - Set the value of all the elements of
countArrray
to 0. - Iterate over the characters of the given string one by one. Increment the value of
countArray
for the ASCII value position by 1. The statementcountArray[inputString[i]]++
is used to increment the value at index positioninputString[i]
which is equal to the ASCII value of the character at positioni
of the stringinputString
. - Print the final sorted string.
- 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 forn
number of times ifn
is the value for that position.
Sample Output :
Enter a string : aldsjldsfalkfdsa
After sorting :
aaadddffjklllsss
Enter a string : helloworld
After sorting :
dehllloorw
Download it on Github
You might also like:
- What is compilation and how compilation process works in C
- C program to check if a string is empty or not
- How to find the LCM of two numbers in C
- C program to check if a character is uppercase without using a library function
- fork() method explanation with example in C
- C program to reverse a string without using any library function(strrev)
- How to use strstr in C with example
- How to split a string in C using strtok library function