C Program to Sort Characters in String
for (int i = 0; str[i] != '\0'; i++) for (int j = i + 1; str[j] != '\0'; j++) if (str[i] > str[j]) { char temp = str[i]; str[i] = str[j]; str[j] = temp; }.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); str[strcspn(str, "\n")] = '\0'; int len = strlen(str); for (int i = 0; i < len - 1; i++) { for (int j = i + 1; j < len; j++) { if (str[i] > str[j]) { char temp = str[i]; str[i] = str[j]; str[j] = temp; } } } printf("Sorted string: %s\n", str); return 0; }
Dry Run
Let's trace the string "hello" through the code to see how characters are sorted.
Initial string
str = "hello"
Compare 'h' with 'e', 'l', 'l', 'o'
'h' > 'e' is true, swap -> str = "ehllo"
Compare 'e' with 'l', 'l', 'o'
'e' < 'l', no swap
Compare first 'l' with second 'l' and 'o'
No swaps needed
Final sorted string
str = "ehllo"
| Iteration | String State |
|---|---|
| Start | hello |
| Swap h and e | ehllo |
| No swap e and l | ehllo |
| No swap l and l | ehllo |
| No swap l and o | ehllo |
Why This Works
Step 1: Reading input
The program reads the string and removes the newline character using fgets and strcspn.
Step 2: Nested loops for comparison
Two loops compare each character with all characters after it to find if any are smaller.
Step 3: Swapping characters
If a character is greater than a later one, they are swapped to move smaller characters forward.
Step 4: Output sorted string
After all swaps, the string is sorted and printed.
Alternative Approaches
#include <stdio.h> #include <stdlib.h> #include <string.h> int cmpfunc(const void *a, const void *b) { return (*(char *)a - *(char *)b); } int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); str[strcspn(str, "\n")] = '\0'; qsort(str, strlen(str), sizeof(char), cmpfunc); printf("Sorted string: %s\n", str); return 0; }
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); str[strcspn(str, "\n")] = '\0'; int len = strlen(str); for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { if (str[j] > str[j + 1]) { char temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp; } } } printf("Sorted string: %s\n", str); return 0; }
Complexity: O(n^2) time, O(1) space
Time Complexity
The nested loops compare each character with others, resulting in O(n^2) time where n is string length.
Space Complexity
Sorting is done in-place, so no extra memory beyond the input string is needed, O(1) space.
Which Approach is Fastest?
Using qsort is faster (average O(n log n)) than manual nested loops (O(n^2)) especially for longer strings.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Nested loops swap | O(n^2) | O(1) | Small strings, simple code |
| Bubble sort | O(n^2) | O(1) | Educational, easy to understand |
| qsort from stdlib.h | O(n log n) | O(1) | Large strings, performance |
qsort from stdlib.h for faster and cleaner sorting of characters.fgets causes unexpected results.