Home »
C++ Programs
C++ program to change the string from lowercase to uppercase using class
Submitted by Shubh Pachori, on August 13, 2022
Problem statement
Given a string, we have to change its case using the class and object approach.
Example:
Input:
Enter String: sHuBh
Output:
Uppercased String: SHUBH
C++ code to change the string from lowercase to lowercase using class and object approach
#include <iostream>
using namespace std;
// create a class
class String {
// private data member
private:
char str[30];
// public functions for string manipulation
public:
// getString() functiom is to get the string
void getString() {
cout << "Enter String:";
cin >> str;
}
// uppercase() function is to manipulate the string
string uppercase() {
// declaring variables for operations
char temp_str[30];
int index;
// for loop to read the whole string and make changes in it
for (index = 0; str[index]; index++) {
// if condition to check if the character is in lowercase
if (str[index] >= 'a' && str[index] <= 'z') {
// if the character is in lowercase then it will be converted to uppercase
temp_str[index] = str[index] - 32;
}
// else it will copied as it is
else {
temp_str[index] = str[index];
}
}
// tranfering null
temp_str[index] = 0;
// return the edited string
return temp_str;
}
};
int main() {
// create a object
String S;
// declare a string variable to store the string
string s;
// object is calling getString() function to get the string
S.getString();
// uppercase() function is called by the object to manipulate it
s = S.uppercase();
cout << "Uppercased String:" << s;
return 0;
}
Output
Enter String:Hello
Uppercased String:HELLO
Explanation
In the above code, we have created a class String, one char type array data member str[30] to store the string, and two public member functions getString() and uppercase() to store and manipulate the given string.
In the main() function, we are creating an object S of class String, reading a string by the user using the function getString(), and finally calling the upperercase() member function to change the case from lowercase to the uppercase of the given string. The uppercase() function contains the logic to change the case of the given string and printing the result.
C++ Class and Object Programs (Set 2) »