按降序对字符串进行排序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 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬