按降序对字符串进行排序C ++
但是,在C++编程中,也可以通过使用字符串排序方法和其他方式适当地执行升序或降序排序。但是在这里,内部和外部遍历循环中涉及的字符串比较(第一个单词与第二个单词)和复制(将第一个单词复制到temp变量中)方法将这些单词按降序排列,如下所示。
示例
#include<bits/stdc++.h>
using namespace std;
int main(){
char str[3][20]={"Ajay","Ramesh","Mahesh"};
char t[20];
int i, j;
for(i=1; i<3; i++){
for(j=1; j<3; j++){
if(strcmp(str[j-1], str[j])>0){
strcpy(t, str[j-1]);
strcpy(str[j-1], str[j]);
strcpy(str[j], t);
}
}
}
cout<<"Sorted in Descending Order ::";
for(i=3; i>=0; i--){
cout<<" ";
cout<<str[i]<<"\n";
}
return 0;
}输出结果
该程序在接受三个单词(Ajay,Ramesh和Mahesh)作为输入之后,通过对字符串进行降序排序来产生结果。
Sorted in Descending Order:: Ramesh Mahesh Ajay
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练