C Programming Example to Sort String to normal sorting ascending

Algorithm


  • We've used string.h header library to calculate string length - strlen().
  • After checking if variable is greater than or not.
  • We've used temporary variable to store the strings in string array.

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <stdio.h>
#include <string.h>

int main (void) {
   char string[] = "HelloCProgrammers";
   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;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
String before sorting - HelloCProgrammers
String after sorting - CHPaeegllmmoorrrs

Demonstration


C Programming Example to Sort String to normal sorting ascending.

Practice C Programming examples online - https://devsenv.com/page/run-c-programming