实现字符串排序,函数指针调用

void sort(char *str,int n,int flag)

参数说明: arr接收实参数组、n接收数组长度
flag接收升降序,如果为1则完成升序,如果为0则是降序
 

  1 #include<stdio.h>
  2 #include<string.h>
  3 void Sort(char *str,int n,int flag){
  4 int count;
  5 char t;
  6 if(flag==1){
  7     for(int i=1;i<n-1;i++){
  8 count=0;
  9 for(int j=0;j<n-i;j++){
 10 if(*(str+j)>*(str+j+1)){
 11 t=*(str+j);*(str+j)=*(str+j+1);*(str+j+1)=t;
 12 count++;
 13 }
 14 }
 15 if(count==0)
 16     break;
 17 }}
 18 else{
 19     for(int i=1;i<n-1;i++){
 20 count=0;
 21 for(int j=0;j<n-i;j++){
 22 if(*(str+j)<*(str+j+1)){
 23 t=*(str+j);*(str+j)=*(str+j+1);*(str+j+1)=t;
 24 count++;
 25 }
 26 }
 27 if(count==0)
 28     break;
 29 }}
 30 puts(str);
 31 }
 32 
 33 int main(int argc, const char *argv[])                                                                                           
 34 {
 35 char a[]="dsqasf";
 36 int s=strlen(a);
 37 int flag;
 38 printf("请输入1升序或0降序\n");
 39 scanf("%d",&flag);
 40 Sort(a,s,flag);
 41     return 0;
 42 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值