按降序对字符串进行排序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