The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Sort String Characters

Here your task is to sort all characters of your entered string in alphabetical order. For example, if user entered a string "orange", you should reply back with a string "aegnor"

Program To Sort Given String

Let us write our own c program to sort the character of a string in alphabetical order

sort-characters.c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
int i, j, len, k = 0, len1;
char text[30], store[30];
printf("Enter a String : ");
gets(text);
len = strlen(text);
printf("Sorted Characters alphabetically : ");
for(i = 65;i <= 90;i++)
{
for(j = 0;j < len;j++)
{
if(text[j] == toupper(i) || text[j] == tolower(i))
{
store[k] = text[j];
k++;
}
}
}
len1 = strlen(store);
for(i = 0;i <= len1;i++)
printf("%c", store[i]);
return 0;
}
Enter a String : apple
Sorted Characters alphabetically : aelpp

Note:

In the following program we use gets() function to get string from our user and then we make use of ASCII code to sort the characters of a string alphabetically i.e) in ascending order from 97 to 122, which gets stored in store[] array. Finally the characters in store[] are displayed sequentially.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report