程序在C中对字符串字符进行排序(Program to sort string characters in C)

优质
小牛编辑
131浏览
2023-12-01

实现 (Implementation)

现在,我们将看到该计划的实际执行情况 -

#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)

该计划的输出应为 -

String before sorting - simplyeasylearning
String after sorting  - aaeegiillmnnprssyy