📝📚Intermediate
Sort a String
Sort string alphabetically
Sort a string alphabetically by sorting its characters.
Program Code
string_sort.c
C
1#include <stdio.h>2#include <string.h>34void sortString(char str[]) {5 int n = strlen(str);6 7 // Bubble sort characters8 for (int i = 0; i < n - 1; i++) {9 for (int j = 0; j < n - i - 1; j++) {10 if (str[j] > str[j + 1]) {11 char temp = str[j];12 str[j] = str[j + 1];13 str[j + 1] = temp;14 }15 }16 }17}1819int main() {20 char str[] = "programming";21 22 printf("Original: %s\n", str);23 24 sortString(str);25 26 printf("Sorted: %s\n", str);27 28 return 0;29}Output
Original: programming
Sorted: aggimmnoprr
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials