
- Learn C By Examples Time
- Learn C by Examples - Home
- C Examples - Simple Programs
- C Examples - Loops/Iterations
- C Examples - Patterns
- C Examples - Arrays
- C Examples - Strings
- C Examples - Mathematics
- C Examples - Linked List
- C Programming Useful Resources
- Learn C By Examples - Quick Guide
- Learn C By Examples - Resources
- Learn C By Examples - Discussion
Program to Sort String Characters in C
Implementation
Now, we shall see the actual implementation of the program −
#include <stdio.h> #include <string.h> int main (void) { char string[] = "simplyeasylearning"; char temp; int i, j; int n = strlen(string); printf("String before sorting - %s \n", string); for (i = 0; i < n-1; i++) { for (j = i+1; j < n; j++) { if (string[i] > string[j]) { temp = string[i]; string[i] = string[j]; string[j] = temp; } } } printf("String after sorting - %s \n", string); return 0; }
Output
Output of this program should be −
String before sorting - simplyeasylearning String after sorting - aaeegiillmnnprssyy
string_programs_in_c.htm
Advertisements