C program to sort characters in 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. The ASCII or American Standard code for Information Interchange is the integer value of each characters like letter, number ,special character etc. 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 value of ‘a’ is 97. Similarly, value of ‘<‘ is 60. Any character you can type has a ASCII value.
Check this website to know all ASCII values

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

Algorithm :

1. To solve this, we will scan the string one time and store the count of each character in an array. For that, first create one integer array of size 256. (Remember, we have 256 ASCII characters).
2. Assign all the elements to 0 of the array.
3. Now, read the string character by character.
4. For each character, increment the value of the array for that character’s ASCII position. For example, if the current character is ‘A’, increment the value on ’65’th position of the array. If one ‘A’ found, final value of this position will become 1, if 10 ‘A’ found, it will become 10.
5. So, after all characters are scanned, the array will hold the count of each character in its specific position.
6.Now, again scan the full array and print that character where its value if more than 0. If array[97] = 2 ,print ’a’ two times.

C program :

#include

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("\n");
    for(i=0 ; i<256; i++){
        printf("%d ",countArray[i]);
    }
    printf("\n");

    //6
    printf(“\nAfter sorting : ");
    
    //7
    for(i=0; i<256 ; i++){
        for(j=0; j<countArray[i]; j++){
            printf("%c",i);
        }
    }
}

Explanation :

1. Create one integer array countArray with size of 256. This will hold the count of each character in 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 store it in the variable inputString.
3. Set the value of all elements of countArrray to 0.
4. Scan all character of the stored string one by one. Increment the value of countArray for the ASCII value position, countArray[inputString[i]] ++ means increment the number of position equal to ASCII value of inputString[i].
5. This step is optional, to show you how the countArray looks like after all characters are scanned. You can remove this step.
6. Print the final sorted string after that.
7. Use one for loop to scan all position of the countArray. For each position, if it is more than 0, print out the character denoted by that specific position.

Sample Output :

Enter a string : aldsjldsfalkfdsa

0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 3 0 2 0 0 0 1 1 3 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
After sorting :
aaadddffjklllsss

Enter a string : helloworld

0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 3 0 0 2 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
After sorting :
dehllloorw

Leave a Reply