C++ program to find the second largest character in the string using class

Given a string, we have to find the second largest character in the string using the class and object approach.
Submitted by Shubh Pachori, on September 06, 2022

Example:

Input:
Enter String : source

Output:
Second largest character is s

C++ code to find the second largest character in the string 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 
  // the string
  void getString() {
    cout << "Enter String : ";
    cin.getline(str, 30);
  }

  // secondLargest() function to find the 
  // second largest character in the string
  char secondLargest() {
    // initialising int type variables to 
    // perform operations
    int index_1, index_2, temp, length;

    // char type variable for manipulation
    char second;

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

    // decrement of 1 in the length
    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;
        }
      }
    }

    // let the first character is the 
    // second largest character
    second = str[0];

    // for loop to for the second largest 
    // character in the string
    for (index_1 = 0; str[index_1]; index_1++) {
      // if condition to check for the second 
      // largest character
      if (str[index_1] < second) {
        // the copying it in the second
        second = str[index_1];

        // and breaking the statement
        break;
      }
    }

    // returning the second largest character
    return second;
  }
};

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

  // char type variable to store 
  // the character
  char second;

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

  // secondLargest() function is called 
  // by the object to find the second 
  // largest character of the string
  second = S.secondLargest();

  cout << "Second largest character is " << second;

  return 0;
}

Output:

RUN 1:
Enter String : Hello world
Second largest character is r

RUN 2:
Enter String : includehelp
Second largest character is p

Explanation:

In the above code, we have created a class String, one char type data member str[30] to store the string, and public member functions  putString() and secondLargest() to store the given string and to find the second largest character.

In the main() function, we are creating an object S of class String, reading the string using the putString() function, and finally calling the secondLargest () member function to find out the second largest character in the given string. The secondLargest () function contains the logic to find out the second largest character in the given string and printing the result.

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





Comments and Discussions!

Load comments ↻






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