C Program to sort string in alphabetical order


In this program, You will learn how to sort string in alphabetical order in c.


List of unsorted string: abc, kbc, xiith, john

List of sorted string in alphabetical order: abc, bck, hiitx, jhno


Example: How to sort string in alphabetical order in c

Run it Now
#include<stdio.h>
#include<string.h>

int main() {

    int i, j, temp;
    char str[100];

    printf("Enter a String :");
    fgets(str, 100, stdin);

    int size = strlen(str);

    for (i = 0; i < size; i++) {
        for (j = i + 1; j < size; j++) {
            if (str[i] > str[j]) {
                temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
        }
    }
    printf("\nSorted String :%s", str);

    return 0;
}

Output:

Enter a String :level
Sorted String :eellv