保研练习题(3)

从键盘上任意输入一个长度不超过 20 的字符串,对所输入的字符串,按照 ASCII 码的大小从小到大进行排序,请输出排序后的结果。

 1 // 算法一
 2 #include<iostream>
 3 #include<string>
 4 #include<algorithm>
 5 using namespace std;
 6 void main()
 7 {
 8     string s;
 9     cout<<"Enter string :";
10     cin>>s;
11     sort(s.begin(),s.end());
12     cout<<s<<endl;
13 }

 1 // 算法二
 2 #include<stdio.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 int cmp(const void *a,const void *b)
 6 {
 7     return *(char *)a-*(char *)b;
 8 }
 9 int main()
10 {
11     printf("Enter string :\n");
12     char s[25];
13     scanf("%s",s);
14     qsort(s,strlen(s),sizeof(s[0]),cmp);
15     puts(s);
16     return 0;
17 }

转载于:https://www.cnblogs.com/raul-ac/p/3323243.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值