题目1066:字符串排序-----------单个字符可以比较大小,用了冒泡排序;另一种用了qsort()

AC:

#include<stdio.h>
#include<stdlib.h>
#include <cstring>

int main()
{
    char *str=(char *)malloc (sizeof(char));
    while(scanf("%s",str)!=EOF)
    {
        int i,j,k;
        int len=strlen(str);
        char s;
        for (i=0;i<len;i++)
         for(j=i;j<len;j++)
          if (str[i]>str[j])
          {
              s=str[i];
              str[i]=str[j];
              str[j]=s;
          } 
        printf("%s
",str);
    }
    return 0;
} 

AC:

#include<stdio.h>
#include<stdlib.h>
#include<cstring>
int cmp(void const *a,void const *b);
int main()
{
    char *str=(char *)malloc (sizeof (char)); 
    while(scanf("%s",str)!=EOF)
    {
        int len=strlen(str);
        qsort(str,len,sizeof(char),cmp);
        printf("%s
",str);
    }
    return 0; 
}

int cmp(void const *a,void const *b)
{
    return *(char *)a-*(char*)b;
}
原文地址:https://www.cnblogs.com/jianrenguo/p/6543189.html