×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to sort the string of characters in descending order using class

Submitted by Shubh Pachori, on September 05, 2022

Problem statement

Given a string of characters, we have to sort its characters in descending order using the class and object approach.

Example:

Input:
Enter String : string

Output:
Sorted String is tsrnig

C++ code to sort the string of characters in descending order using the class and object approach

#include <iostream>
using namespace std;

// create a class
class String {
  // private data member
 private:
  char str[30];

  // public functions
 public:
  // getString() function to 
  // insert string
  void getString() {
    cout << "Enter String : ";
    cin.getline(str, 30);
  }

  // sortDescending() function to sort the 
  // string in descending order
  string sortDescending() {
    // initialising int type variables to 
    // perform operations
    int index_1, index_2, temp, length;

    // for loop to finding the length of the string
    for (length = 0; str[length]; length++)
      ;

    // decrement in the length of 1
    length--;

    // for loop to read the whole string
    for (index_1 = 0; str[index_1]; index_1++) {
      // for loop to compare characters of the string
      for (index_2 = 0; index_2 < length - index_1; index_2++) {
        // if condition to check if the next character 
        // is smaller than
        // this then swapping takes place
        if (str[index_2] > str[index_2 + 1]) {
          // swapping character if character
          // are not in the order
          temp = str[index_2];
          str[index_2] = str[index_2 + 1];
          str[index_2 + 1] = temp;
        }
      }
    }
    // returning the sorted string
    return str;
  }
};

int main() {
  // create a object
  String S;

  // string type variable to 
  // store the string
  string str;

  // function is called by the object to 
  // store the string
  S.getString();

  // sortDescending() function is called 
  // by the object to sort the string in 
  // descending order
  str = S.sortDescending();

  cout << "Sorted String is " << str;

  return 0;
}

Output

RUN 1:
Enter String : abcdfrgxyz
Sorted String is zyxrgfdcba

RUN 2:
Enter String : Alexander
Sorted String is xrnleedaA

Explanation

In the above code, we have created a class String, one char type string data member str[30] to store the string, and public member functions  getString() and sortDescending() to store the given string and to sort the string in descending order.

In the main() function, we are creating an object S of class String, reading the string using the getString() function, and finally calling the sortDescending() member function to sort the given string. The sortDescending() function contains the logic to sort the given string in descending order  and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.