Home »
C++ Programs
C++ program to find the second smallest character in the string using class
Submitted by Shubh Pachori, on September 06, 2022
Problem statement
Given a string, we have to find the second smallest character in the string using the class and object approach.
Example:
Input:
Enter String : source
Output:
Second smallest character is e
C++ code to find the second smallest 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);
}
// secondSmallest() function to find
// the second smallest character in the
// string
char secondSmallest() {
// 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 greater 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 smallest character
second = str[0];
// for loop to for the second smallest character
// in the string
for (index_1 = 0; str[index_1]; index_1++) {
// if condition to check for the second
// smallest character
if (str[index_1] > second) {
// the copying it in the second
second = str[index_1];
// and breaking the statement
break;
}
}
// returning the second smallest 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();
// secondSmallest() function is called by the object to
// find the second smallest character of the string
second = S.secondSmallest();
cout << "Second smallest character is " << second;
return 0;
}
Output
RUN 1:
Enter String : Hello world
Second smallest character is H
RUN 2:
Enter String : includehelp
Second smallest character is d
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 secondSmallest() to store the given string and to find the second smallest 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 secondSmallest() member function to find out the second smallest character in the given string. The secondSmallest() function contains the logic to find out the second smallest character in the given string and printing the result.
C++ Class and Object Programs (Set 2) »